Re: instance vs class attributes

2005-03-29 Thread 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:

 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/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instance vs class attributes

2005-03-29 Thread James Stroud
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/
--
http://mail.python.org/mailman/listinfo/python-list


Re: instance vs class attributes

2005-03-29 Thread Brian van den Broek
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: instance vs class attributes

2005-03-29 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: instance vs class attributes

2005-03-29 Thread James Stroud
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/
-- 
http://mail.python.org/mailman/listinfo/python-list