Does Python really follow its philosophy of "Readability counts"?

Terry Reedy tjreedy at udel.edu
Tue Jan 20 22:41:57 EST 2009


Duncan Booth wrote:
> Luis Zarrabeitia <kyrie at uh.cu> wrote:
> 
>> It boggles me when I see python code with properties that only set and
>> get the attribute, or even worse, getters and setters for that
>> purpose. In my university they teach the students to write properties
>> for the attributes in C# ("never make a public attribute, always write
>> a public property that just gets and sets it"). I never understood
>> that practice either, given that the syntax for attribute access and
>> property access in C# is exactly the same. (Could it be that even if
>> the syntax is the same, the compiled code differs? Don't know enough
>> about .NET to answer that). 
>>
> The compiled code differs.

I *strongly* doubt that.  Properties are designed to be transparent to 
user code that access atrributes through the usual dotted name notation 
precisely so that class code can be changed from
   x = ob
to
   x = property(get_x, set_x, del_x)
without changing user code.

CPython compiles attribute access to
<load object>
LOAD_ATTR <attr_name>

The code associated with LOAD_ATTR should get the attribute object and 
check whether it is a property, in which case it calls the proper method 
of the property.

 > That doesn't matter if the class is only
> accessed from within a single compiled program, but it does matter if you 
> expose a public interface from a library: if you change a public attribute 
> into a property then you need to recompile all code which accesses it.

Example to back this claim?  Perhaps you have found a bug.

tjr




More information about the Python-list mailing list