instance vs class attributes

S

Sarir Khamsi

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
 
J

James Stroud

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:
.... a = 2
.... def get_a(self):
.... return self.a
.... def set_a(self,an_a):
.... self.a = an_a
........ 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/
 
J

James Stroud

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:
... a = 2
... def get_a(self):
... return self.a
... def set_a(self,an_a):
... self.a = an_a
...4

James

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

http://www.jamesstroud.com/
 
B

Brian van den Broek

James Stroud said unto the world upon 2005-03-29 20:37:
Sarir said:




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.


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:
'''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 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 on method _shy_method in module __main__:

_shy_method(self) unbound __main__.Shy method
I'm a shy docstring

Traceback (most recent call last):
File "<pyshell#25>", line 1, in -toplevel-
shy.__mangled_method()
AttributeError: Shy instance has no attribute '__mangled_method'
Best,

Brian vdB
 
R

runsun pan

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
 
J

James Stroud

You need 2 underscores to mangle.

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/
 
D

Dennis Lee Bieber

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().

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,230
Messages
2,571,161
Members
47,796
Latest member
AlphonseNa

Latest Threads

Top