473,666 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1291
On 21 Oct 2006 01:17:32 -0700, ph*******@gmail .com <ph*******@gmai l.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_acti on()
return self._a
def _set_a(self, val):
some_other_addi tional_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_addi tional_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.c om/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==behavi our" /
"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
3603
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 thing you know ;-) After discovering that access 2000 support form properties (I'm a
11
18830
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 they are too big and wind up covering up some of the fields on the main form. Is there any good code out there that works in a similar fashion that will also either a) stretch the form width wise on widescreens or b), rely on height rather than...
1
1769
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 FormA Inherits System.Windows.Forms.Form Private Sub Button_Click(ByVal etc., etc.) Handles btnCalculate.Click FormB.Textbox.Text = "some updated value" End Sub
3
1550
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 are things like "addiction", "criminal", "dangerous?" etc. Total number of categories = 20). For EACH of the 20 categories there are 5 statuses, from "bad" (1=worst) to "good" (5=best). (Therefore, there are really 100 choices: 20 categories...
6
2555
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 Is it generally considered good practice to issue the command in line #2440 - intended to go to a new record in the vehicle entry form - from frmMainMenu?
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.