On Thu, 11 Oct 2007 18:58:44 +0200, Bruno Desthuilliers wrote:
[snip]

Your implementation seems particularly broken. You do not return anything 
from `name()`, hereby removing name as an attribute (or: replacing it 
with its return value -- None). You should return ``property(**locals())
`` (or ``property(fget=fget, fset=fset, ...)``, whatever you like).

I'm going to point out a few other mistakes first:

> class Toto(object):
>    def __iinit__(self, name):

Typo here: __init__

>      self.name = name
>    @apply
>    def name():
>      def fget(self):
>        print "getting %s.name" % self
>        return self._name
>      def fset(self, val):
>        print "setting %s.name to %s" % (self, val) 
>        self._name = name

It should be `val`, not `name`, huh? And, as mentioned above, the return 
value is missing.

>    def say_hello(self):
>      print "Hello, my name is %s" % self.name

A fixed implementation could be something along these lines::

    >>> class Toto(object):
    ...    def __init__(self, name):
    ...      self.name = name
    ...    @apply
    ...    def name():
    ...      def fget(self):
    ...        print "getting %s.name" % self
    ...        return self._name
    ...      def fset(self, val):
    ...        print "setting %s.name to %s" % (self, val)
    ...        self._name = val
    ...      return property(**locals())
    ...    def say_hello(self):
    ...      print "Hello, my name is %s" % self.name
    ...
    >>> t = Toto("bruno")
    setting <__main__.Toto object at 0xb792f66c>.name to bruno
    >>> t.say_hello()
    getting <__main__.Toto object at 0xb792f66c>.name
    Hello, my name is bruno
    >>> t.name
    getting <__main__.Toto object at 0xb792f66c>.name
    'bruno'
    >>> t.name = "jon"
    setting <__main__.Toto object at 0xb792f66c>.name to jon
    >>> t.say_hello()
    getting <__main__.Toto object at 0xb792f66c>.name
    Hello, my name is jon

Cheers,
Stargaming
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to