Skip to content

Instantly share code, notes, and snippets.

@datamafia
Created December 11, 2014 19:40
Show Gist options
  • Save datamafia/dde429b8b812f70a12df to your computer and use it in GitHub Desktop.
Save datamafia/dde429b8b812f70a12df to your computer and use it in GitHub Desktop.
A few ways to handle kwargs.
# A few ways to handle kwargs, notes form the field.
class simple(object):
def __init__(self, **kwargs):
print 'simple.__init__()'
print 'Kwargs :'
print kwargs
t = simple()
t = simple(key='v4lue')
print '\r\n'
class proper(object):
def __init__(self, **kwargs):
print 'proper.__init__()'
print 'Kwargs :'
print kwargs
for i in kwargs:
setattr(self,i,kwargs[i])
t = proper()
t = proper(key='v4lue')
print t.key
print '\r\n'
class ideal(object):
kwargs = {}
def __init__(self, **kwargs):
print 'ideal.__init__()'
self.kwargs = kwargs
t = ideal()
t = ideal(key='v4lue')
print 'print t.kwargs["key"] : ' + t.kwargs["key"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment