Using attrs for everything in Python
glyph.twistedmatrix.com
Using attrs for everything in Python
1–10 of 105 posts
Re: Using attrs for everything in Python
#2This seems nice, but other than easy destructuring of functions that return long tuples (which really should return dicts), I cannot figure out a good use case for it over using a dictionary.
I am keen to hear others' opinions.
Re: Using attrs for everything in Python
#3This seems nice, but other than easy destructuring of functions that return long tuples (which really should return dicts), I cannot figure out a good use case for it over using a dictionary. I am keen to hear others' opinions.
A class with more methods than the Point3d example.
Re: Using attrs for everything in Python
#4This is how I develop in Python. I do not use this helper library (and will certainly look into it), but definitely wish that more Python hackers built software in this way. Passing plain-old classes around that are very small and close to their domain is hugely valuable. Adding convenience methods or synthetic properties with a @property decorator is also hugely important. It lends itself to a very nice DSL where, for example, you can have image.png where the png property is a literal PNG representation of the image.
Re: Using attrs for everything in Python
#5The short names confuse me a bit on this.
Re: Using attrs for everything in Python
#6The short names confuse me a bit on this.
Re: Using attrs for everything in Python
#7Would there be any value to allowing a convention for the __init__() method that allows direct assignment of arguments to attributes? For example, replacing the following:
class Point3D(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
with: class Point3D(object):
def __init__(self, self.x, self.y, self.z):
# Other initialization work
It seems __init__ could be made to parse the list of parameters, and for any self.foo that's not defined, assign the corresponding argument to that attribute. But I have no experience in language design. Is there a reason this wouldn't work?Re: Using attrs for everything in Python
#8Would there be any value to allowing a convention for the __init__() method that allows direct assignment of arguments to attributes? For example, replacing the following: class Point3D(object): def __init__(self, x, y, z): self.x = x self.y = y self.z = z with: class Point3D(object): def __init__(self, self.x, self.y, self.z): # Other initialization work It seems __init__ could be made to parse the list of parameter…
you can just do
class B:
def __init__(self, a, b, c):
self.__dict__.update(locals())Re: Using attrs for everything in Python
#9Re: Using attrs for everything in Python
#10While the conciseness of attrs is nice, it's not very readable.
This:
class Point3D(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
...is much easier to read than this: import attr
@attr.s
class Point3D(object):
x = attr.ib()
y = attr.ib()
z = attr.ib()
Readability is what I love about Python. I don't think the conciseness of attrs is worth the loss in readability.