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

instance vs class attributes

P: n/a
I come from a C++ background and am learning some of the details of
Python's OO capability and now have some questions. Given:

#!/bin/env python

class A(object):
_x = 10
def __init__(self): self.x = 20
def square(self): return self.x * self.x

print 'A.x = %d' % A._x
a = A()
print 'a.x = %d' % a.x
print 'a.square() = %d' %a.square()

I get the output:

A.x = 10
a.x = 20
a.square() = 400

Here are my questions:

1) What are the benefits of using a member variable without the 'self'
qualifier? (I think this is because you can use _x without an
instance of A().)

2) Is there a preferred coding style (_x vs self.x)?

3) Should private data be written as self._x instead of self.x?

Thanks.

Sarir
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sarir said:
Here are my questions:

1) What are the benefits of using a member variable without the 'self'
qualifier? (I think this is because you can use _x without an
instance of A().)
No such thing as a benefit here. self.a inside a method is the same as a
outside (see code below for what I mean).
2) Is there a preferred coding style (_x vs self.x)?
This is essentially a non sequitur.
3) Should private data be written as self._x instead of self.x?


This is a long standing debate. The usual answer is "we are all grownups
here", meaning that self.x is preferred. However, I personally like self._x
to let potential users of my code know that I intended it to be private.

The code I was talking about:
class bob: .... a = 2
.... def get_a(self):
.... return self.a
.... def set_a(self,an_a):
.... self.a = an_a
.... abob = bob()
abob.get_a() 2 abob.a 2 abob.set_a(14)
abob.a 14 abob.get_a() 14 class carol:

.... self.a = 22
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in carol
NameError: name 'self' is not defined
James

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 18 '05 #2

P: n/a
On Tuesday 29 March 2005 05:37 pm, James Stroud wrote:
1) What are the benefits of using a member variable without the 'self'
* *qualifier? (I think this is because you can use _x without an
* *instance of A().)


No such thing as a benefit here. self.a inside a method is the same as a
outside (see code below for what I mean).


By the way, here is some unintuitive behavior to think about:
class bob: ... a = 2
... def get_a(self):
... return self.a
... def set_a(self,an_a):
... self.a = an_a
... bob1 = bob()
bob1.a 2 bob1.set_a(4)
bob1.a 4 bob.a = 14
bob1.a 4 bob2 = bob()
bob2.a 14 bob.a = 99
bob2.a 99 bob1.a

4

James

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 18 '05 #3

P: n/a
James Stroud said unto the world upon 2005-03-29 20:37:
Sarir said:
Here are my questions:
<SNIP>
3) Should private data be written as self._x instead of self.x?

This is a long standing debate. The usual answer is "we are all grownups
here", meaning that self.x is preferred. However, I personally like self._x
to let potential users of my code know that I intended it to be private.


<SNIP>
James


My understanding is that there is no such thing as true privacy with
Python, since you can always access self._name and self.__mangled_name
if you want to. (Though the later way of naming makes it a bit
difficult so as to strongly indicate it is discouraged Example below.)
Instead of private names, think of them as shy :-)

But I don't think it is correct to say the self.x form is preferred.
Some support tools, notably pydoc, understand convention that single
and double leading underscores means the name is shy and respect it:
class Shy: '''My silly example'''
def _shy_method(self):
'''I'm a shy docstring'''
pass
def public_method(self):
'''I'm a public docstring'''
pass
def __mangled_method(self):
'''I'm quite shy and a bit of a challenge to call'''
print "I've been mangled!"
help(Shy) Help on class Shy in module __main__:

class Shy
| My silly example
|
| Methods defined here:
|
| public_method(self)
| I'm a public docstring
You can still use pydoc on the shy methods, though:
help(Shy._shy_method) Help on method _shy_method in module __main__:

_shy_method(self) unbound __main__.Shy method
I'm a shy docstring
# calling a mangled method name from outside the class
shy = Shy()
shy.__mangled_method()
Traceback (most recent call last):
File "<pyshell#25>", line 1, in -toplevel-
shy.__mangled_method()
AttributeError: Shy instance has no attribute '__mangled_method' shy._Shy__mangled_method() Ive been mangled!


Best,

Brian vdB

Jul 18 '05 #4

P: n/a
>>> shy._Shy__mangled_method()
Ive been mangled!
Hi Brian,

can you explain how this could possibly work? First of all it's not
standard python usage,
and secondly it's not working on my computer...

pan

Jul 18 '05 #5

P: n/a
You need 2 underscores to mangle.

On Tuesday 29 March 2005 09:58 pm, runsun pan wrote:
shy._Shy__mangled_method()


Ive been mangled!
Hi Brian,

can you explain how this could possibly work? First of all it's not
standard python usage,
and secondly it's not working on my computer...

pan


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 18 '05 #6

P: n/a
On Tue, 29 Mar 2005 17:34:50 -0700, Sarir Khamsi
<sa**********@raytheon.com> declaimed the following in comp.lang.python:
1) What are the benefits of using a member variable without the 'self'
qualifier? (I think this is because you can use _x without an
instance of A().)

class A(object):
_x = 10
def __init__(self):
self._x = 20
def square(self):
return (self._x * self._x, A._x * A._x)
def newval(self, vl):
A._x = vl #note no self...
a = A()
b = A()

print a.square()
print b.square()
b.newval(30)
print a.square()
print b.square()
-=-=-=-=-=-=-=-
(400, 100)
(400, 100)
(400, 900)
(400, 900)

The one without the self. prefix is shared by all instances of
A() objects. Note how changing A._x from within b.newval() also changed
the value computed by a.square(), not just that of b.square().

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.