Re: [Python-Dev] Definining properties - a use case for classdecorators?

2005-10-19 Thread Guido van Rossum
On 10/19/05, Fredrik Lundh [EMAIL PROTECTED] wrote:
 letting class inject a slightly magic self variable into the class 
 namespace ?

 class C:

 foo = property(self.getFoo, self.setFoo, None, 'the foo property')

 def getFoo(self):
 return self._foo

 def setFoo(self, foo):
 self._foo = foo

 (figuring out exactly what self should be is left as an exercise etc)

It's magical enough to deserve to be called __self__. But even so:

I've seen proposals like this a few times in other contexts. I may
even have endorsed the idea at one time. The goal is always the same:
forcing delayed evaluation of a getattr operation without using either
a string literal or a lambda. But I find it quite a bit too magical,
for all values of xyzzy, that xyzzy.foo would return a function of one
argument that, when called with an argument x, returns x.foo. Even if
it's easy enough to write the solution (*), that sentence describing
it gives me goosebumps. And the logical consequence, xyzzy.foo(x),
which is an obfuscated way to write x.foo, makes me nervous.

(*) Here's the solution:

class XYZZY(object):
def __getattr__(self, name):
return lambda arg: getattr(arg, name)
xyzzy = XYZZY()

--
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Definining properties - a use case for classdecorators?

2005-10-16 Thread Delaney, Timothy (Tim)
Guido van Rossum wrote:

 To which Tim Delaney responded, have a look at my response here:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408713
 
 I looked at that, and now I believe it's actually *better* to mention
 the property name twice, at least compared to Tim' s approach.

I never said it was a *good* approach - just *an* approach ;)

Tim Delaney
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com