473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

setters and getters in python 2.6 and 3.0

Hi list, I've been following a discussion on a new way of defining
getters and setters on python-dev and just can't understand what the
purpose is. Everybody agreed on the dev list that this is a good idea
so I guess it must be right :)

The whole thing started with this post of Guido:

http://mail.python.org/pipermail/pyt...er/075057.html

which then continued into November. Basically, the idea is that using
the new way a setter can be added to property that was read-only
before. But if I have this already,

class C:
@property
def attr( self ): return self._attr

what prevents me using the following for adding a setter for attr:

class C:
def attr( self ): return self._attr
def set_attr( self, value ): self._attr = value
attr = property( attr, set_attr )

In other words all I needed to do is delete @property, write the
setter method and add attr = property( attr, set_attr ). What does the
new way improve on this?
Nov 29 '07 #1
2 1898
Daniel Fetchinson schrieb:
Hi list, I've been following a discussion on a new way of defining
getters and setters on python-dev and just can't understand what the
purpose is. Everybody agreed on the dev list that this is a good idea
so I guess it must be right :)

The whole thing started with this post of Guido:

http://mail.python.org/pipermail/pyt...er/075057.html

which then continued into November. Basically, the idea is that using
the new way a setter can be added to property that was read-only
before. But if I have this already,

class C:
@property
def attr( self ): return self._attr

what prevents me using the following for adding a setter for attr:

class C:
def attr( self ): return self._attr
def set_attr( self, value ): self._attr = value
attr = property( attr, set_attr )

In other words all I needed to do is delete @property, write the
setter method and add attr = property( attr, set_attr ). What does the
new way improve on this?
It prevents namespace-pollution in a clever way. By first defining the
getter, the @propset-decorator will augment the already createt property
and return it.

Thus you don't end up with a

set_attr

function.
Other, more complex recipes to do the same look like this and are much
harder to grasp:
@apply
def my_property()
def fget(self):
return self._value
def fset(self, value):
self._value = value
return property(**locals())

So the proposed propset-decorator certainly makes things clearer.

Diez
Nov 29 '07 #2
Hi list, I've been following a discussion on a new way of defining
getters and setters on python-dev and just can't understand what the
purpose is. Everybody agreed on the dev list that this is a good idea
so I guess it must be right :)

The whole thing started with this post of Guido:

http://mail.python.org/pipermail/pyt...er/075057.html

which then continued into November. Basically, the idea is that using
the new way a setter can be added to property that was read-only
before. But if I have this already,

class C:
@property
def attr( self ): return self._attr

what prevents me using the following for adding a setter for attr:

class C:
def attr( self ): return self._attr
def set_attr( self, value ): self._attr = value
attr = property( attr, set_attr )

In other words all I needed to do is delete @property, write the
setter method and add attr = property( attr, set_attr ). What does the
new way improve on this?

It prevents namespace-pollution in a clever way. By first defining the
getter, the @propset-decorator will augment the already createt property
and return it.

Thus you don't end up with a

set_attr

function.
Other, more complex recipes to do the same look like this and are much
harder to grasp:
@apply
def my_property()
def fget(self):
return self._value
def fset(self, value):
self._value = value
return property(**locals())

So the proposed propset-decorator certainly makes things clearer.

Diez
Aaaaaha :)
Makes sense indeed.

Thanks,
Daniel
Nov 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: kelvSYC | last post by:
I'm familiar with get and set function paradigms from Java, but what's the recommended design for such in C++? Should it be like so: foo& getFoo(); void setFoo(foo& f); or like so: foo&...
2
by: Lachlan Hunt | last post by:
Hi, In JavaScript 1.5, objects can use special getter and setter functions for properties. However, these only seem to be implemented in Gecko and, AFAICT, don't seem to be part of ECMAScript. ...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.