change property after inheritance

Steven Bethard steven.bethard at gmail.com
Fri Sep 8 13:16:09 EDT 2006


Maric Michaud wrote:
> Le jeudi 07 septembre 2006 15:33, Steven Bethard a écrit :
>> Well, lambda's not going away[1],
> 
> Sure, they won't.
> 
>> but there's no *need* for lambda here. 
>>   It could be written as::
> 
> Le jeudi 07 septembre 2006 17:16, George Sakkis a écrit :
>> Sure, it *could*; whether it *should* is a different issue. I can't
>> imagine a case for absolute *need* of lambda, but there are several
>> cases where it is probably the best way, such as the one of this
>> thread.
> 
> I have no preferences here, I used lambdas because it's more compact but they 
> have also their drawback, when the function get a little more complex the 
> code is quickly confusing. The main advantage of the lambdas in this case is 
> to not pollute the class namespace.
> 
> Le jeudi 07 septembre 2006 23:48, Steven Bethard a écrit :
>>  Try using one of the following recipies:
>>
>>      http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408713
>>      http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442418
> 
> The code i wrote was to demonstrate late binding is usually not needed (and 
> it's not the semantic of properties so it's a bit like "make Java in 
> Python").

If you're really this uncomfortable with writing your own descriptors, 
sure, you don't have to.  But descriptors are an integral part of Python 
-- new-style classes wouldn't have methods without them, nor could 
classmethod or staticmethod have been defined.  So defining a new 
descriptor is far from un-Pythonic.

> If you really want late binding, the first recipe may be a solution, but it 
> should be both simpler and should not introduce a new semantic (the functions 
> passed as strings is disappointing).

If you want to use the functions instead of their names, it's as simple 
as changing the __init__ to:

     def __init__(self, fget=None, fset=None, fdel=None, doc=None):
         self.getname = fget.__name__
         self.setname = fset.__name__
         self.delname = fdel.__name__
         self.__doc__ = doc

Then you can use the same signature as property.

STeVe



More information about the Python-list mailing list