<- Back | Index | Forward ->
Object Attributes
- Attributes are members of objects,
- You can "get" attributes or "set" them,
- Python attributes are resolved at run-time,
- __getattr__ is called if a "get" lookup fails,
- __setattr__ is called to "set" an attribute to a new value.
We use this to make toolkit function calls look like attributes:
class Atom:
def __init__(self, handle):
self.handle = handle
def __getattr__(self, name):
if name == "charge":
return dt_charge(self.handle)
raise AttributeError, name
def __setattr__(self, name, val):
if name == "charge":
return dt_setcharge(self.handle, val)
self.__dict__[name] = val
a = Atom(12)
print a.charge
a.charge = 2