Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Default arguments in Python - the return - running out of ideas but...
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
CTO  
View profile  
 More options May 14 2009, 5:32 am
From: CTO <debat...@gmail.com>
Date: Thu, 14 May 2009 02:32:14 -0700 (PDT)
Local: Thurs, May 14 2009 5:32 am
Subject: Re: [Python-ideas] Default arguments in Python - the return - running out of ideas but...

> I should have said "it's impossible short of looking at the source
> code or doing some very sophisticated introspection of the bytecode of
> the module the function is defined in".

Any which way you slice this it will require that literal code *not*
be interpreted until execution time. There are other ways to do that-
storing it in strings, as George Sakkis does, modifying the language
itself, as is the proposal here, or reading and parsing the original
source. But you're right- more info is needed than what the bytecode
contains.

> Even so, your recipe doesn't quite work in several cases, aside from
> when the source code is not accessible.

Obviously, you are quite correct. Scoping in particular is difficult
both to understand and to properly handle- had me chasing my tail
for about twenty minutes earlier, actually- and I'm sure this is a
security nightmare, but it does (generally) what is being asked for
here. And it does so without recourse to changing the syntax.

Here's another possible mechanism:

def runtime(f):
        """Evaluates a function's annotations at runtime."""
        annotations = getfullargspec(f)[-1]
        @wraps(f)
        def wrapped(*args, **kwargs):
                defaults = {k: eval(v) for k, v in annotations.items()}
                defaults.update(kwargs)
                return f(*args, **defaults)
        return wrapped

@runtime
def example1(x, y:'[]'):
        y.append(x)
        return y

@runtime
def example2(x:'a**2+2*b+c'):
        return x

Pretty simple, although it messes with the call syntax pretty
badly, effectively treating a non-keyword argument as a
keyword-only argument. There's probably a way around that
but I doubt I'm going to see it tonight.

The point is, I don't really see the point in adding a new
syntax. There are *lots* of incomplete solutions floating
around to this issue, and it will probably take a lot less
work to make one of those into a complete solution than it
will to add a new syntax, if that makes any sense at all.

Also, do you mind posting any problems you find in that
to the activestate message board so there is a record
there?

Geremy Condra
_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.