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
 
Arnaud Delobelle  
View profile  
 More options May 14 2009, 4:08 am
From: Arnaud Delobelle <arno...@googlemail.com>
Date: Thu, 14 May 2009 09:08:13 +0100
Local: Thurs, May 14 2009 4:08 am
Subject: Re: [Python-ideas] Default arguments in Python - the return - running out of ideas but...
2009/5/14 CTO <debat...@gmail.com>:

> Thanks for the input, but I've already written the code to do this.
> It
> is available at <URL:http://code.activestate.com/recipes/576751/>.

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".

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

def nesting():
    default = 3
    @runtime
    def example3(x=default):
        return x
    example3()

nesting()

@runtime
def name(x=a):
    return x

name()

* The first one fails because default is not a global variable, thus
not accessible from within the runtime decorator.  I don't know how if
this can be fixed.  Note that for the function to exec() at all, you
need to e.g. modify remove_decorators so that it also removes initial
whitespace, something like:

def remove_decorators(source):
    """Removes the decorators from the given function"""
    lines = source.splitlines()
    lines = [line for line in lines if not line.startswith('@')]
    indent = 0
    while lines[0][indent] == ' ':
        indent += 1
    new_source = '\n'.join(line[indent:] for line in lines)
    return new_source

* The second one fails because of a clash of names.  I guess that can
be fixed by specifying what the locals and globals are explicitely in
the calls to exec and eval.

--
Arnaud
_______________________________________________
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.