Re: [Zope3-dev] keeping attributes abstract

2006-03-09 Thread Benji York

Sam Stainsby wrote:

So I'm inclines to use the IFooB approach by default to achieve more
abstract attributes. Am I missing something here in the IFooA approach
that allows me to do this anyway? 


You are :)  Python has properties.  They give you a good migration path 
from a simple attribute to what you would otherwise have to use 
getter/setters for.  An example would help.  Today you do this:


class Foo(Persistent):
implements(IFooB)

bar = None

And then you decide, oops I need to calculate bar every time it's 
accessed.  Then you can do this:


class Foo(Persistent):
implements(IFooB)

@property
def bar(self):
return time.time() * 2 # crazy thing, but just an example

But that bar is read-only, what if you also want to be able to set bar, 
you can do this:


class Foo(Persistent):
implements(IFooB)

@apply
def bar(self):
doc = The bar attribute

def fset(self, bar):
if bar is None:
self._bar = 47
else:
self._bar = bar

def fget(self):
return self._bar * 44

return property(**locals)

Some people like a more explicit version of the last line, if so you can 
use return property(fset, fget, None, doc).


(WARNING: I didn't actually run any of the code in this message, typos 
may have crept in)

--
Benji York
Senior Software Engineer
Zope Corporation
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



Re: [Zope3-dev] keeping attributes abstract

2006-03-09 Thread Stephan Richter
On Wednesday 08 March 2006 23:52, Sam Stainsby wrote:
 No 'bar' attribute needed!

I'll note that in the Python community we consider it an advantage of using 
attributes/properties versus accessor and mutator methods. This distinction 
between an attribute- versus method-centric object oriented programming 
language has been discussed in many articles.

Note that we provide several other mechanisms to extend functionality; for 
example, the event system. Overall Zope (like most other Python code) is 
optimized to work with attributes/properties. We just like them too much! :-)

Regards,
Stephan
-- 
Stephan Richter
CBU Physics  Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com



Re: [Zope3-dev] keeping attributes abstract

2006-03-09 Thread Sidnei da Silva
On Thu, Mar 09, 2006 at 07:11:20AM -0500, Stephan Richter wrote:
| On Wednesday 08 March 2006 23:52, Sam Stainsby wrote:
|  No 'bar' attribute needed!
| 
| I'll note that in the Python community we consider it an advantage of using 
| attributes/properties versus accessor and mutator methods. This distinction 
| between an attribute- versus method-centric object oriented programming 
| language has been discussed in many articles.
| 
| Note that we provide several other mechanisms to extend functionality; for 
| example, the event system. Overall Zope (like most other Python code) is 
| optimized to work with attributes/properties. We just like them too much! :-)

Not to mention that you can still use accessors/mutators with plain
attributes, and then store the actual value under a different name to
avoid the clash:

class FooX(Persistent):
implements(IFooB)

_bar = None

def setBar(self, bar):
doSomething()
self._bar = bar


def getBar(self):
doSomethingElse()
return self._bar

bar = property(getBar, setBar)

-- 
Sidnei da Silva
Enfold Systems, Inc.
http://enfoldsystems.com
___
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com