[Python-Dev] [Python 2.2 BUG] pickle/cPickle does not find __slots__

Burton Radons loth@users.sourceforge.net
Fri, 15 Feb 2002 17:05:00 -0800


Fred L. Drake, Jr. wrote:

> Michael McLay writes:
>  > While my approach was patterened after the property() builtin, the
>  > Python Labs crowd didn't like the notation and rejected the
> 
> I'll note as well that at least some of us, if not all, don't like the
> property() syntax as well.  My current favorite was one of Guido's
> proposals at Python 10:
> 
> 
> class Foo(object):
>     property myprop:
>         """A computed property on Foo objects."""
> 
>         def __get__(self):
>             return ...
>         def __set__(self):
>             ...
>         def __delete__(self):
>             ...


What's wrong with:

   class Foo(object):
     class myprop_class (object):
       """A computed property on Foo objects."""

       def __get__(subclass, self, klass):
         return ...
       def __set__(subclass, self, value):
         ...
       def __delete__(subclass, self):
         ...

     myprop = myprop_class ()

It's a grand total of _two_ lines more than your example, and has more 
extensions possibilities to boot.

While we're discussing strange usages of blocks, one feature I've always 
wanted has been subblocks passed to functions.  It could be done using a 
new argument prefix "@" (for example).  If the block is not otherwise 
taken (if it is in an if statement, for example), it can be followed by 
":" and a normal block; this block is then put in the argument as a 
PyCodeObject (I think).  The argument can also be given a value 
normally.  The code object also has a few new methods for our convenience.

So to implement your example above:

   def field (@block):
     dict = block.exec_save_locals () # Execute the block and return the 
locals dictionary rather than destroy it.
     fget = dict.get ("__get__", None)
     fset = dict.get ("__set__", None)
     fdel = dict.get ("__delete__", None)
     fdoc = dict.get ("__doc__", None)

     return property (fget, fset, fdel, fdoc)

Now that we have that, we do your example:

   class Foo(object):
     myprop = field ():
       """A computed property on Foo objects."""

       def __get__(self):
         return ...
       def __set__(self, value):
         ...
       def __delete__(self):
         ...

There are other capabilities, but as I've never had a language that can 
do this I wouldn't know how many pragmatic possibilities there are.  The 
advantage over Guido's method is that his suggestion solves a single 
problem and has no use outside of it, while mine, at least on the face 
of it, could be applied in other ways.