Parameter lists
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Sun Feb 4 12:06:23 EST 2007
Mizipzor
> I dont like the looks of that code, there are many
> function parameters to be sent in and if I were to add an attribute, i
> would need to add it in three places. Add it to the function
> parameters, add it to the class and assign it.
> Is there a smoother way to do this?
You may use something like this:
def selfassign(self, locals):
# Code from web.py http://webpy.org modified.
for key, value in locals.iteritems():
if key != 'self':
setattr(self, key, value)
Generally used in __init__ methods, as:
def __init__(self, foo, bar, baz=1):
selfassign(self, locals())
You may use it as (untested):
class Stats:
def __init__(self, speed, maxHp, armor, strength, attackSpeed,
imagePath):
selfassign(self, locals())
self.originalImage = loadTexture(imagePath)
del self.imagePath
I don't like that del
Bye,
bearophile
More information about the Python-list
mailing list