473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Best way to set/get an object property

Hey,
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?
What is Python preferred method to do so (using the new feature
'property')?
I don't think __getattr__ and __setattr__ are practical (I have to
code the property name into them).
Thanks.
Aug 24 '08 #1
8 2501
Hussein B wrote:
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?
What is Python preferred method to do so (using the new feature
'property')?
I don't think __getattr__ and __setattr__ are practical (I have to
code the property name into them).
Hussein, I don't think you'll learn much from asking these abstract
questions. At some point you have to get your hands dirty and write actual
code to get a feel for the language.

For example, it will then become obvious for you that property works best
for individual attributes while __getattr__ and friends are more convenient
if you want to treat multiple attributes the same way, attributes whose
names may not even be known until runtime (think delegation).

Peter
Aug 24 '08 #2
On Aug 24, 5:28 am, Peter Otten <__pete...@web.dewrote:
Hussein B wrote:
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?
What is Python preferred method to do so (using the new feature
'property')?
I don't think __getattr__ and __setattr__ are practical (I have to
code the property name into them).

Hussein, I don't think you'll learn much from asking these abstract
questions. At some point you have to get your hands dirty and write actual
code to get a feel for the language.

For example, it will then become obvious for you that property works best
for individual attributes while __getattr__ and friends are more convenient
if you want to treat multiple attributes the same way, attributes whose
names may not even be known until runtime (think delegation).

Peter
Thanks Peter,
You are right, I have to try to touch the Python but the problem is I
don't have much time to do so.
I have a Java developer for more than 4 years and I find it is not so
easy to digest Python concepts, this is why I'm asking a lot of
obvious and clear easy to you (long time Pythonists).
Thank you for your time.
Aug 24 '08 #3
On Aug 24, 5:07*am, Hussein B <hubaghd...@gmail.comwrote:
Hey,
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?
What is Python preferred method to do so (using the new feature
'property')?
I don't think __getattr__ and __setattr__ are practical (I have to
code the property name into them).
Thanks.
The answer Hussein is you have both options in Python. If neither one
is clearly better-suited to your new application, pick one and go.
Aug 24 '08 #4
On Sun, 24 Aug 2008 12:28:53 +0200, Peter Otten wrote:
Hussein B wrote:
>I noted that Python encourage the usage of: --
obj.prop = data
x = obj.prop
--
to set/get an object's property value. What if I want to run some logic
upon setting/getting a property? What is Python preferred method to do
so (using the new feature 'property')?
I don't think __getattr__ and __setattr__ are practical (I have to code
the property name into them).

Hussein, I don't think you'll learn much from asking these abstract
questions. At some point you have to get your hands dirty and write
actual code to get a feel for the language.

For example, it will then become obvious for you that property works
best for individual attributes while __getattr__ and friends are more
convenient if you want to treat multiple attributes the same way,
attributes whose names may not even be known until runtime (think
delegation).

I think you are misunderstanding Hussein's question. I believe that he is
using "property" to refer to what we would call an attribute. Naturally I
could be wrong, but this is how I interpret his question.

I think the actual answer to his question is that properties are the
preferred way to "run some logic upon setting/getting" an attribute, that
is, to implement getters and setters.

Hussein, the Java habit of writing setters and getters for everything
isn't considered good practice in Python, but if you need them, that's
exactly what the property() function is for.

--
Steven
Aug 25 '08 #5
On Aug 24, 7:12 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Sun, 24 Aug 2008 12:28:53 +0200, Peter Otten wrote:
Hussein B wrote:
I noted that Python encourage the usage of: --
obj.prop = data
x = obj.prop
--
to set/get an object's property value. What if I want to run some logic
upon setting/getting a property? What is Python preferred method to do
so (using the new feature 'property')?
I don't think __getattr__ and __setattr__ are practical (I have to code
the property name into them).
Hussein, I don't think you'll learn much from asking these abstract
questions. At some point you have to get your hands dirty and write
actual code to get a feel for the language.
For example, it will then become obvious for you that property works
best for individual attributes while __getattr__ and friends are more
convenient if you want to treat multiple attributes the same way,
attributes whose names may not even be known until runtime (think
delegation).

I think you are misunderstanding Hussein's question. I believe that he is
using "property" to refer to what we would call an attribute. Naturally I
could be wrong, but this is how I interpret his question.

I think the actual answer to his question is that properties are the
preferred way to "run some logic upon setting/getting" an attribute, that
is, to implement getters and setters.

Hussein, the Java habit of writing setters and getters for everything
isn't considered good practice in Python, but if you need them, that's
exactly what the property() function is for.

--
Steven
Thank you Steven :)
--
public class JClass {
private int answer; // property
}
--
class PyClass(object):
doc __init__(self):
self.answer = None
--
AFAIUY (understand you), what it is called a property in Java, it is
called an attribute in Python?
Why Python encourages direct access to object's attributes? aren't
setters/getters considered vital in OOP (encapsulation)?
Thank you all for your time and help.
Aug 25 '08 #6
On Aug 25, 4:56*pm, Hussein B <hubaghd...@gmail.comwrote:
AFAIUY (understand you), what it is called a property in Java, it is
called an attribute in Python?
Why Python encourages direct access to object's attributes?
The simplest answer is "Because Python is not Java" :)

Speaking of which, have you read the blog post of the same name? It
might be useful given your Java background: http://dirtsimple.org/2004/12/python-is-not-java.html
>*aren't
setters/getters considered vital in OOP (encapsulation)?
Not at all. They're definitely part of the mechanism that Java
provides for encapsulation, sure. However, because Python provides a
consistent interface for accessing attributes and properties, you
don't need to define a property unless your code requires it. If all
your getters & setters are doing is reading & writing to an attribute,
then why not just r&w directly to the attribute? If you later need to
add more complexity to that process, you can easily create a property
without having to change how any other piece of code refers to that
property, given it shares the same interface with attributes.
Aug 25 '08 #7
This is probably what you want:

http://www.python.org/download/relea...ntro/#property

Available in Python 2.2 or later.

Enjoy,
Ken Seehart

Hussein B wrote:
Hey,
I noted that Python encourage the usage of:
--
obj.prop = data
x = obj.prop
--
to set/get an object's property value.
What if I want to run some logic upon setting/getting a property?
What is Python preferred method to do so (using the new feature
'property')?
I don't think __getattr__ and __setattr__ are practical (I have to
code the property name into them).
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list

Aug 25 '08 #8
Steven D'Aprano a écrit :
(snip)
But it's quite rare to see double-underscore "really private" attributes
in Python code. It is considered to go against the spirit of the language.
Not necessarily "against the spirit" - it's mostly than __name_mangling
is only really useful when you want to protect a really vital
implementation attribute from being *accidentaly* overridden, and mostly
annoying anywhere else.
I'm told that in Java it is quite difficult to change a class from using
public attributes to getters/setters,
That's an understatement. Java has *no* support for computed attributes,
so you just can *not* turn a public attribute into a computed one.
and therefore many Java developers
prefer to use getters/setters right from the beginning.
Truth is that they have no other choice if they want to be able to
decouple implementation from interface.

Aug 26 '08 #9

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

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
1
by: zee | last post by:
I am new to web services and would like to create a web service wrapper around a COM API which is large and requires a log in. The log in is quite process intensive and shouldn't be done too often....
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.