Immutability

vatamane at gmail.com vatamane at gmail.com
Sat Jul 1 04:23:09 EDT 2006


That is why I don't like the use of @property.  Even though the
decorator is a nice short-hand notation, it is more confusing in my
oppinion.  So someone is more likely still call the property as a
function when looking at:

class C:
   @property
   def data(self):
       return 42

rather than if they saw:

class C:
   def _data(self):
       return 42
   data=property(_data)

I personally I find the second case more readable, even though it is
longer.

-Nick


Fredrik Lundh wrote:
> Nick Maclaren wrote:
>
> > |> a property looks like an attribute, not a method, so you're trying to call whatever
> > |> your "joe()" method returns.
> >
> > Well, yes, that was pretty obvious - but what was NOT obvious is why it
> > should do that for one of two identical methods.
>
> identical?  you only applied @property to one of the methods, and then you're
> surprised that only one of the methods were turned into a property?
>
> > |> (that's what "a function for getting an attribute value" in the property documentation
> > |> refers to).
> >
> > Well, as joe is an attribute of the class fred, and the decorator is applied
> > to the declaration of joe within fred, I assumed that it referred to getting
> > joe from fred.  That certainly doesn't appear to be the case
>
> @property turns your "joe" method into a getter method for the (virtual) attribute
> "joe".  when you access the attribute, the getter method will be called.  whatever
> that method returns will be the attribute's value.  that's what the documentation
> says, and that's what your code is doing.
> 
> </F>




More information about the Python-list mailing list