By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,582 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

Good Form

P: n/a
I am new to Python but come from a C++ background so I am trying to
connect the dots :) . I am really liking what I see so far but have
some nubee questions on what is considered good form. For one thing I
am used to class variables being accessable only through methods
instaed of directly refrenced from the object instence. From what I
have read it looks like when you access a class variable directly in
Python it has something in the background that works similar to a
getter of setter.

Would you normally write methods to retrive and set your class
variables or just refrence them directly?

Oct 21 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 21 Oct 2006 01:17:32 -0700, ph*******@gmail.com <ph*******@gmail.comwrote:
I am new to Python but come from a C++ background so I am trying to
connect the dots :) . I am really liking what I see so far but have
some nubee questions on what is considered good form. For one thing I
am used to class variables being accessable only through methods
instaed of directly refrenced from the object instence. From what I
have read it looks like when you access a class variable directly in
Python it has something in the background that works similar to a
getter of setter.

Would you normally write methods to retrive and set your class
variables or just refrence them directly?
It's largely a matter of taste and context I think. For instance, you
can trust the user of your code to leave read-only variables read-only
in most cases. I don't think there really is any absolutely sure way
of introducing private attributes (__ prefix just name-mangles). In
other cases, it seems more logical to have 'virtual' attributes, a few
canonical examples being temperature conversion and exchange rates;
imagine an object that has a number of dollars, Euros, yen, or other
major currency as an attribute and uses methods to give the equivalent
value in other currencies.

-- Theerasak
Oct 21 '06 #2

P: n/a
ph*******@gmail.com schrieb:
Would you normally write methods to retrive and set your class
variables or just refrence them directly?
you start by referencing them directly and ONLY if you need you can add
getters and setters later on without breaking any client code.
see the property function.

An explanation for the motivation behind this and python's thinking can
be found here:

http://tinyurl.com/wfgyw

--
Servus, Gregor
Oct 21 '06 #3

P: n/a
Hi Phez

Generally, most Python programmers I know access and set class
attributes directly. This is done because of a Python feature called
property().

In many languages, setting class attributes directly is discouraged
because additional behavior may need to be associated with that setting,
and it may be difficult or impossible to later inject this behavior into
the setting action.

In Python, however, if we have a class like

class A:
a = 1

we just get and set a like:
>>obj = A()
obj.a = 2
If we later want to associate additional behavior with setting A.a, we
can do the following:

class A(object):
_a = 1
def _get_a(self):
additional_action()
return self._a
def _set_a(self, val):
some_other_additional_action()
_a = val
a = property(_get_a, _set_a)

Now when we do
>>obj = A()
obj.a = 2
obj.a = 2 will call _set_a(self, 2), which in this case will run
some_other_additional_action() and then set a to 2.

Gregor's post contains prudent advice on when to use this property()
business (that is, only when definitely needed :) ).

Best,

Travis Vachon
ph*******@gmail.com wrote:
I am new to Python but come from a C++ background so I am trying to
connect the dots :) . I am really liking what I see so far but have
some nubee questions on what is considered good form. For one thing I
am used to class variables being accessable only through methods
instaed of directly refrenced from the object instence. From what I
have read it looks like when you access a class variable directly in
Python it has something in the background that works similar to a
getter of setter.

Would you normally write methods to retrive and set your class
variables or just refrence them directly?

Oct 21 '06 #4

P: n/a
ph*******@gmail.com writes:
I am new to Python but come from a C++ background so I am trying to
connect the dots :)
Welcome, and commiserations on your harsh upbringing :-)
I am really liking what I see so far but have
some nubee questions on what is considered good form.
This document is an official proposal for "good style" for code in the
Python standard library. Most consider it a good style reference for
all Python code.

<URL:www.python.org/dev/peps/pep-0008/>
For one thing I am used to class variables being accessable only
through methods instaed of directly refrenced from the object
instence. From what I have read it looks like when you access a
class variable directly in Python it has something in the background
that works similar to a getter of setter.
This document was written specifically for those coming from Java, but
many of its points would also be applicable to the C++ mindset.

<URL:http://dirtsimple.org/2004/12/python-is-not-java.html>

This one responds to the above, and specifically addresses getters and
setters.

<URL:http://simon.incutio.com/archive/2004/12/03/getters>

Here's a couple more, also responding to the same article.

<URL:http://naeblis.cx/rtomayko/2004/12/15/the-static-method-thing>
<URL:http://naeblis.cx/articles/2005/01/20/getters-setters-fuxors>

--
\ "It is forbidden to steal hotel towels. Please if you are not |
`\ person to do such is please not to read notice." -- Hotel |
_o__) sign, Kowloon, Hong Kong |
Ben Finney

Oct 21 '06 #5

P: n/a

ph*******@gmail.com a écrit :
I am new to Python but come from a C++ background so I am trying to
connect the dots :) . I am really liking what I see so far but have
some nubee questions on what is considered good form. For one thing I
am used to class variables
I assume you mean "instance attributes". In Python, "class attributes"
are attributes that belong to the class object itself, and are shared
by all instances.
>being accessable only through methods
instaed of directly refrenced from the object instence.
C++ have "access restriction" on attributes, which default is
"private". Also, C++ makes a strong distinction between fonctions and
data. Python's object model is very different here:
1/ There's no language-inforced access restriction. Instead, there's a
naming convention stating that names starting with an underscore are
implementation detail - ie : not part of the public interface - and as
such *should* not be accessed by client code.
2/ Since Python is 100% object, functions (and methods) are objects too
- so there's no strong "member variable vs method" distinction : both
are attributes.
From what I
have read it looks like when you access a class variable directly in
Python it has something in the background that works similar to a
getter of setter.
That's not technically true. *But* since Python has support for
computed attributes, it's quite easy to turn a raw attribute into a
computed one without the client code noticing it. So from a conceptual
POV, you can think of direct attribute acces as a computed attribute
acces with implicit getter/setter...
Would you normally write methods to retrive and set your class
variables or just refrence them directly?
Depends. Languages like C++ or Java not having support for computed
attributes, the tradition is to have a "public methods==behaviour" /
"private data==state" mindset. With Python's support for both computed
attributes and functions as first class objects, thinks are quite
differents. The best approach IMHO is to think in terms of "public
interface vs implementation", and choose to use attribute or method
syntax based on semantic - how it will be effectively implemented being
an orthogonal concern.

To make things short : if it's obviously an attribute (ie : person's
name etc), start by making it a public attribute and access it
directly. If and when you need to control access to this attribute
(either to compute it or make it read-only or whatever), then turn it
into a property (aka computed attribute).

Oct 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.