I already talked about destructuring in JavaScript/Python/Kotlin in this previos post. So, to sum up, we can use destructuring with any iterable Python object (that's equivalent to JavaScript Array destructuring). That's nice, but it would be event more cute to have something that we could use with any object (a bit in the vein of JavaScript Object destructuring) with no need to make it iterable. I've been looking into some possibilities:
If you are working with simple dataclasses the astuple() function comes pretty handy. Notice though, that it will recursivelly retrieve elements of other dataclasses, lists, dictionaries... which probably is not what you want./p>
@dataclass
class Person:
name: str
city: str
post_code: int
age: int
job: str
p1 = Person("Iyan", "Xixon", 33200, 49, "Coder")
name, _, pc, job, _ = astuple(p1)
print(name, pc, job)
#Iyan 33200 Coder
That approach works in a positional way. We know the order of the the attributes of the class and we take those positions we want and discard others (with the _ convention).
Another option, this one more nominal, as we take attributes based on their name, is using operator.attrgetter, like this:
name, pc, job = operator.attrgetter("name", "post_code", "job")(p1)
print(name, pc, job)
#Iyan 33200 Coder
It looks ok, but using attribute names in its string form is a huge risk. If you rename an attribute refactoring tools will know nothing about your string, and you have to remember that you are accessing that attribute as a string to manually fix it. It's easy to make a mess... With that in mind, I think I prefer the redundancy of writing the object name multiple times:
name, pc, job = p1.name, p1.post_code, p1.job
print(name, pc, job)
#Iyan 33200 Coder
Related to this, what if we want to transform each of the values we are destructuring, but with a different transformation for each value? I'm not talking about a normal map() call where we apply the same funcion, I'm talking about applying different functions. For that, a transform function like this will do the trick:
def transform(funcs, items):
return [func(item) for func, item in zip(funcs, items)]
That way we can write something like this:
name, pc, job = transform(
(str.upper, lambda x: x, str.lower),
attrgetter("name", "post_code", "job")(p1),
)
print(name, pc, job)
#IYAN 33200 coder
name, pc, job = transform(
(str.upper, lambda x: x, str.lower),
(p1.name, p1.post_code, p1.job),
)
print(name, pc, job)
IYAN 33200 coder
No comments:
Post a Comment