I hate typing out every 'self.x = x' line in an init method. Is this alternative acceptable?
class Movable:
def init(self, x, y, dx, dy, worldwidth, worldheight):
"""automatically sets the given arguments. Can be reused with any class that has an order of named args."""
nonmembers = #populate with names that should not become members and will be used later. In many simple classes, this can be left empty.
for key, value in list(locals().items())1:: #exclude 'self', which is the first entry.
if not key in nonmembers:
setattr(self, key, value)
#handle all nonmembers and assign other members:
return
I always hate how redundant and bothersome it is to type "self.member = member" 10+ times, and this code does work the way I want it to. It's
/r/Python
https://redd.it/1b5bc8g
class Movable:
def init(self, x, y, dx, dy, worldwidth, worldheight):
"""automatically sets the given arguments. Can be reused with any class that has an order of named args."""
nonmembers = #populate with names that should not become members and will be used later. In many simple classes, this can be left empty.
for key, value in list(locals().items())1:: #exclude 'self', which is the first entry.
if not key in nonmembers:
setattr(self, key, value)
#handle all nonmembers and assign other members:
return
I always hate how redundant and bothersome it is to type "self.member = member" 10+ times, and this code does work the way I want it to. It's
/r/Python
https://redd.it/1b5bc8g
Reddit
From the Python community on Reddit
Explore this post and more from the Python community