News for hackers. Stuff that matters.
Re:Using attrs for everything in Python by japhyr on Thursday August 25, 2016 @12:19PM (#12360268)
Would 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 by dlbucci on Thursday August 25, 2016 @12:51PM (#12360508)
CoffeeScript has a feature like that, where a constructor can call parameters `@whatever` and they will be assigned to the object. For all the ways CoffeeScript borrows from Python, but somehow makes it worse, that feature is actually very nice/useful (and not super confusing, either).
Re:Using attrs for everything in Python by m_ke on Thursday August 25, 2016 @12:28PM (#12360335)
you can just do

  class B:
      def __init__(self, a, b, c):
          self.__dict__.update(locals())
Re:Using attrs for everything in Python by jsmthrowaway on Thursday August 25, 2016 @12:50PM (#12360498)
You really, really, really shouldn't do this under any circumstances, though. If you find yourself messing with __dict__ you're down a hole, if you find yourself mutating it you're down an even deeper hole, if you find yourself calling locals() you probably forgot your shovel, and I'd hope that for all of that you'd have a better reason than minimizing code (that's way less readable, for example). I immediately get suspicious when I read code that interacts with __dict__ and I'm only sympathetic if you're cooking a metaclass or something equally arcane.

__slots__ also makes __dict__ nonexistent.

Re:Using attrs for everything in Python by chc on Thursday August 25, 2016 @12:58PM (#12360571)
Well, yeah, but you could implement it without __dict__ just by doing something like:

  for attr, val in locals().items():
    setattr(self, attr, val)
It still feels pretty un-Pythonic either way.
Re:Using attrs for everything in Python by jsmthrowaway on Thursday August 25, 2016 @01:04PM (#12360616)
Because it is. There is zero reason to be clever here. Yeah, you have to be explicit in __init__. We are all used to it. The "a," "b," "c" examples are disingenuous though because you're often validating, enforcing types, converting, deriving other attributes...

__init__ is documentation. I read it to understand what's going on.

Re:Using attrs for everything in Python by desdiv on Thursday August 25, 2016 @02:34PM (#12361288)
That makes B.self a field too, which you probably don't want.
Re:Using attrs for everything in Python by xapata on Thursday August 25, 2016 @10:26PM (#12363942)
Especially because that creates a circular reference and annoys the garbage collector.
Re:Using attrs for everything in Python by m_ke on Thursday August 25, 2016 @07:02PM (#12363157)
yeah I know, I was wondering if anyone would notice