Skip to content

Instantly share code, notes, and snippets.

@qgp9
Last active October 28, 2016 07:43
Show Gist options
  • Save qgp9/c00c53137c8c7da7ea4628235218cd87 to your computer and use it in GitHub Desktop.
Save qgp9/c00c53137c8c7da7ea4628235218cd87 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
def load_on_default( obj, pargs ):
for k, v in pargs.items():
if( hasattr( obj, k ) ):
setattr(obj, k, v )
else:
print( "WARNING: the key \"{}\" is wrong.".format(k) )
class MyClass:
def __init__(self,**pargs):
self.a = 1
self.b = 2
load_on_default(self,pargs)
myobj = MyClass(a=2,b=3)
print( 'a={}\tb={}'.format(myobj.a,myobj.b) )
myobj = MyClass(a=4,d=5)
print( 'a={}\tb={}'.format(myobj.a,myobj.b) )
a=2 b=3
WARNING: the key "d" is wrong.
a=4 b=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment