[Tutor] Re: q about method(self)
Magnus Lycka
magnus@thinkware.se
Mon, 02 Sep 2002 10:31:42 +0200
At 10:39 2002-09-01 -0400, Erik Price wrote:
>On Sunday, September 1, 2002, at 02:05 AM, Magnus Lycka wrote:
>
>>At 09:28 2002-08-31 -0700, Emile van Sebille wrote:
>>>Normally, you probably wouldn't.
>>
>>Certainly with inheritence. For instance:
>>
>>class A:
>> def __init__(self, name):
>> self.name = name
>>
>>class B(A):
>> def __init__(self, name, address):
>> A.__init__(self, name)
^^^^^^^^^^^^^^^^^^^^^^
>> self.address
>>
>>This is a silly, simplistic example of cource, but there are
>>certainly a lot of cases where subclasses want to call methods
>>in superclasses.
>
>I'm sorry, I should have been more explicit. I meant to say "But what I'm
>wondering is, why/when would you want to use the method(inst_ref, args)
>form rather than the more traditional inst_ref.method(args) form?"
See above. When you are in an instance of one class, and
call a method of a base class, i.e. another class this
class inherited from.
Let's imagine a system with objects where an attribute is
automatically updated with a timestamp and a user identity
whenever an attribute is changed. *Some* objects also have
a policy that only the owner of the object must change it.
...
class ChangeLoggedObjects:
def __setattr__(self, attr, value):
self.__dict__['changeTimeStamp'] = time.time()
self.__dict__['changedBy'] = getCurrentUser()
self.__dict__['attr'] = value
...
class ChangeRestrictedObjects(ChangeLoggedObjects):
def __setattr__(self, attr, value):
if getCurrentUser() != self.owner:
raise PermissionError
ChangeLoggedObjects.__setattr__(self, attr, value)
If ChangeRestrictedObjects.__setattr__ wouldn't call
ChangeLoggedObjects.__setattr__ you wouldn't update change
parameters for change restricted objects.
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se