[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: Use cases for del
From:       Grant Edwards <grante () visi ! com>
Date:       2005-07-07 4:38:32
Message-ID: 11cpce8shb7si77 () corp ! supernews ! com
[Download RAW message or body]

On 2005-07-07, George Sakkis <gsakkis@rutgers.edu> wrote:

> I guess he means why not define foo as property:
>
> class demo(object):
>     foo = property(fget = lambda self: self.v,
>                    fset = lambda self,v: setattr(self,'v',v))
>
> d = demo()
> d.foo = 3
> print d.foo

In some ways that's even cleaner.

In my simplivied example all the foo() method was doing was
setting/returning an attribute, but usually there's a bit more
going on (e.g. system calls with possible side effects).  

To me it's a bit more explicit that

  d.foo(3)
  d.foo()

are doing something other than just getting/setting the value
of an instance attribute.  If all I really wanted to do was
get/set the instance's "v" attribute, I probably would have
just done it like this

  d.v = 3
  print d.v  

-- 
Grant Edwards                   grante             Yow!  .. I wonder if I
                                  at               ought to tell them about my
                               visi.com            PREVIOUS LIFE as a COMPLETE
                                                   STRANGER?
-- 
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic