Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def applykwargs(kwargs,key,classobject):
- """kwargs is a dictionary of the form: {'arg1':val1,....}
- key is a string for the desired key to (potentially) find in kwargs, and update in classobject"""
- try:
- kwargsvalue = kwargs[key] # if key exists in kwargs, kwargsvalue will be set to it
- setattr(classobject,key,kwargsvalue) # set the appropriate value in classobject
- except:
- # key does not exist in kwargs
- pass
- # For you though, looping is better
- # say you call thisFunction(linear=True,title="awesomeness")
- # and you make your class have:
- # to set all your vars, you want to do...well first of all, from the function call, we have
- kwargs={'linear' : True, 'title' : "awesomeness"}
- # so do this:
- class myClass:
- linear = False
- title = ''
- legLoc = 2
- for key, value in kwargs.iteritems(): #the iteritems let's you pick each entry one at a time, setting both the key and value
- # Set correct var in your class
- setattr(myClass,key,value)
- # That's it! Now...
- print myClass.linear # will be True
- print myClass.title # will be "awesomeness"
- print myClass.legLoc # will *still* be 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement