[Tutor] new object attribute from string?
alan.gauld@bt.com
alan.gauld@bt.com
Mon Apr 14 12:37:05 2003
> It is possible to dynamically add new attributes to our objects. Each
> object in Python has an internal '__dict__' dictionary that
> keeps track of all of its attributes:
>
> ###
> >>> class TestAttribute:
> ... def __init__(self, name):
> ... self.name = name
> ... def printInternals(self):
> ... print "my dictionary looks like this:", self.__dict__
>
> And we're already very familiar with dictionaries: we can
> easily add new attributes by assigning another key to
> that __dict__ionary.
But since we can achieve the same by simply assigning to a
new attribute there is no need for such complexity
>>> t1.question = 'new object attribute from string?'
Achieves the same result
> >>> t1.question
> 'new object attribute from string?'
Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/