Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
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