Skip to content

Instantly share code, notes, and snippets.

@skanev
Created August 24, 2012 11:45
Show Gist options
  • Save skanev/3449665 to your computer and use it in GitHub Desktop.
Save skanev/3449665 to your computer and use it in GitHub Desktop.
Set arguments passed to the constructor as attributes
# This is a fairly simple way to do it, but I dislike it:
class Person:
def __init__(self, name, age, location):
self.__dict__.update({k: v for (k, v) in locals().items() if k != 'self'})
person = Person('Stefan', 26, 'Sofia')
print(person.name)
print(person.age)
print(person.location)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment