is it possible to give an instance a value?

Subscriber123 subscriber123 at gmail.com
Wed Mar 7 05:29:49 CET 2007


In answer to this part of your question, how to make this work:

>>>Person.Address
> 'Sydney'
> >>>Person.Address.type
> '%String'
> >>>Person.Address = 'Canberra'
> >>>print Person.Address. Person.Address.type
> Canberra %String
>

try using the __getattr__ method of the class:

>>> class objWithTypeAttr(object):
...     def __getattr__(self,key):
...         if key is not "type":
...             return self.__dict__[key]
...         else:
...             return type(self.val)
...     def __init__(self,val):
...         self.val=val
...
>>> a=objWithTypeAttr("blah")
>>> a
<__main__.objWithTypeAttr object at 0x00AE0CD0>
>>> a.val
'blah'
>>> a.type
<type 'str'>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070306/b2394d1f/attachment.html>


More information about the Python-list mailing list