Created
December 11, 2014 19:40
-
-
Save datamafia/dde429b8b812f70a12df to your computer and use it in GitHub Desktop.
A few ways to handle kwargs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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