[Python-Dev] Extended Function syntax

Neal Norwitz neal@metaslash.com
Thu, 30 Jan 2003 12:06:07 -0500


On Thu, Jan 30, 2003 at 08:00:59AM -0500, Guido van Rossum wrote:
> 
> How about
> 
>   foo = property:
>       ...

So far, it seems there are 3 constructs we have talked about improving:

 * classmethod, staticmethod
 * property
 * thunks on arbitrary blocks which could help locking, e.g.

I'm not sure any one (proposed) solution can solve all of these.
I don't really like any of the proposals so far.  The reason
is that none obviously demonstrate the semantics.

Currently, this obviously defines a function.

        def foo(): # ...

mwh/sameule's proposals for:

        def foo() [staticmethod]: # ...

is fairly clear.  I suppose it could be extended for threading/locks
at the function level:

        def foo() [staticmethod, synchronized]: # ... 

One could also define pre- and post- conditions on a method
basis with user-defined callables:

        def foo() [precond, postcond]: # ...

This doesn't seem optimal, but it's also the best I've seen so far.

I'm not sure how useful it would be to extend to classes though:

        class foo(base) [singleton]:

is one case I can think of.  However, it doesn't seem worth 
extending the syntax to save one line from time to time.
Are there many other potential uses?

I don't like Guido's proposal for properties because
the intent is not clear:

        foo = property:
            ...

What is the code block?  What is the block required to do?
Writing the above seems awkward and error-prone.  But
perhaps I'm just being too conservative.

I'm not even sure if I like this version better:

        def foo property:
            ...

However, using def is more obvious to me what's going on.

The last issue could provide many benefits, but also seems
to be the hardest.  One could do:

        synchronize(lockvar):
            ...

Where lockvar has certain attributes acquiring/releasing
a lock.  But if we wanted to extend it to a file:

        synchronize(somefile):
            ...

doesn't seem very obvious.  The "synchronize" keyword doesn't
make sense.  "with" is more general, but I don't think it's
very clear to say:

        with(somefile):
            ...

or 
        with(lockvar):
            ...

But even if this was decent, how would one operate on the
variable?  Special methods __prewith__ and __postwith__?

I'm just adding some fuel to the fire.  I don't particularly
like any of these suggestions either. :-)

Neal