assign class variable in __init__

danieldelay danieldelay at gmail.com
Tue Jun 8 14:17:12 EDT 2010


you can also use :

 >>> class A(object):
	def __init__(self, **args):
		self.__dict__.update(args)

 >>> a=A(source='test', length=2)
 >>> a.source
'test'

but this is to be used carefully because mispelling your args somewhere 
in your program will not raise any error :
 >>> A(sourc='test', lenth=2)

Daniel



More information about the Python-list mailing list