473,320 Members | 1,990 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,320 software developers and data experts.

Good Form

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
5 1268
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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Raposa Velha | last post by:
Hello to all! Does any of you want to comment the approach I implement for instantiating a form? A description and an example follow. Cheers, RV jmclopesAThotmail.com replace the AT with the...
11
by: Jozef | last post by:
I have some old code that I use from the Access 95 Developers handbook. The code works very well, with the exception that it doesn't seem to recognize wide screens, and sizes tab controls so that...
1
by: trebor | last post by:
I'm learning dotNet, although I first learned programming back in the days when structured programming was all the rage. In both my books and courses, I've seen something like this: Public Class...
3
by: Strasser | last post by:
In a nested subform in datasheet view, an interviewer of homeless people picks a descriptive CATEGORY from 20 descriptive categories. The 20 categories are displayed via a combo box. (Categories...
6
by: MLH | last post by:
I have frmMainMenu with the following two code lines invoked on a button click... 2420 DoCmd.OpenForm "frmVehicleEntryForm", A_NORMAL, , , A_ADD, A_NORMAL 2440 DoCmd.GoToRecord , , A_NEWREC ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.