function v. method

Leif K-Brooks eurleif at ecritters.biz
Tue Jul 18 03:31:44 EDT 2006


danielx wrote:
> This is still a little bit of magic, which gets me thinking again about
> the stuff I self-censored. Since the dot syntax does something special
> and unexpected in my case, why not use some more dot-magic to implement
> privates? Privates don't have to be entirely absent from Klass.__dict__
> (which would make Python not introspective); they can just be invisible
> when using the dot-syntax.

You can do this now, kind of:

 >>> class Foo(object):
...  x = property()
...  def doStuffWithX(self):
...   self.__dict__['x'] = 123
...   print self.__dict__['x']
...
 >>> bar = Foo()
 >>> bar.doStuffWithX()
123
 >>> bar.x
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: unreadable attribute

If you're proposing that self.x and bar.x should give different results, 
then that's quite a bit more magic than property() and methods use. They 
both use the descriptor API; for more information on that, read 
<http://python.org/download/releases/2.2.3/descrintro/>.



More information about the Python-list mailing list