[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Function attributes

Gabriel Genellina

2/11/2010 11:37:00 PM

En Thu, 11 Feb 2010 00:25:00 -0300, Terry Reedy <tjreedy@udel.edu>
escribió:
> On 2/10/2010 4:49 PM, Gabriel Genellina wrote:
>
>> I've written a decorator for "injecting" a __function__ name into the
>> function namespace, but I can't find it anywhere. I think I implemented
>> it by adding a fake additional argument and replacing LOAD_GLOBAL with
>> LOAD_NAME in the bytecode.
>
> The decorator only needs to replace the defaults args tuple.
> It does not even need to know the parameter name,
> just that it is the only (or last) with a default .
>
> def f(n, me=None):
> if n > 0: return n*me(n-1)
> elif n==0: return 1
>
> f.__defaults__ = (f,) # 3.1
> print(f(5))

This is simple to implement, but requires changing the function
definition. My goal was to keep the original code unchanged, that is,
leave it as:

def f(n):
if n > 0: return n*f(n-1)
elif n==0: return 1

(like a normal, recursive function), and make the 'f' inside the function
body "magically" refer to the function itself.

> Of course, user could still screw up recursion by providing another
> value for 'me'. This strikes me as about a likely (low) as a user
> screwing up recursion in a library module function by rebinding the name
> in the imported module.

Well, if people really want to shoot themselves in the foot, there's
nothing we can do to avoid that...

--
Gabriel Genellina

7 Answers

Arnaud Delobelle

2/12/2010 7:29:00 AM

0

"Gabriel Genellina" <gagsl-py2@yahoo.com.ar> writes:

> En Thu, 11 Feb 2010 00:25:00 -0300, Terry Reedy <tjreedy@udel.edu>
> escribió:
>> On 2/10/2010 4:49 PM, Gabriel Genellina wrote:
>>
>>> I've written a decorator for "injecting" a __function__ name into the
>>> function namespace, but I can't find it anywhere. I think I implemented
>>> it by adding a fake additional argument and replacing LOAD_GLOBAL with
>>> LOAD_NAME in the bytecode.
>>
>> The decorator only needs to replace the defaults args tuple.
>> It does not even need to know the parameter name,
>> just that it is the only (or last) with a default .
>>
>> def f(n, me=None):
>> if n > 0: return n*me(n-1)
>> elif n==0: return 1
>>
>> f.__defaults__ = (f,) # 3.1
>> print(f(5))
>
> This is simple to implement, but requires changing the function
> definition. My goal was to keep the original code unchanged, that is,
> leave it as:
>
> def f(n):
> if n > 0: return n*f(n-1)
> elif n==0: return 1
>
> (like a normal, recursive function), and make the 'f' inside the function
> body "magically" refer to the function itself.

I posted an example of a decorator that does just this in this thread a
couple of days ago:

http://mail.python.org/pipermail/python-list/2010-February/12...

It doesn't require any bytecode hacking, although it requires breaking
apart the function object and making a new one from the bits.

--
Arnaud

Gabriel Genellina

2/13/2010 1:49:00 AM

0

En Fri, 12 Feb 2010 04:29:12 -0300, Arnaud Delobelle
<arnodel@googlemail.com> escribió:

> I posted an example of a decorator that does just this in this thread a
> couple of days ago:
>
> http://mail.python.org/pipermail/python-list/2010-February/12...

Ouch! I didn't see your post, nor several other earlier posts in this
thread. In fact, I thought mine was the *first* reply when I wrote it!

Yes, of course, your code does exactly that without any bytecode hacks.

--
Gabriel Genellina

Mark Lawrence

2/13/2010 11:02:00 AM

0

Gabriel Genellina wrote:
> En Fri, 12 Feb 2010 04:29:12 -0300, Arnaud Delobelle
> <arnodel@googlemail.com> escribió:
>
>> I posted an example of a decorator that does just this in this thread a
>> couple of days ago:
>>
>> http://mail.python.org/pipermail/python-list/2010-February/12...
>
> Ouch! I didn't see your post, nor several other earlier posts in this
> thread. In fact, I thought mine was the *first* reply when I wrote it!
>

It's not just me then. I'm using Thunderbird on Windows Vista to view
the group via gmane, and notice the earlier parts of a thread can show
up hours or even days after the later ones. Would something who
understands these things be kind enough to explain why this happens, or
at least provide the keywords needed for google, as I haven't got a clue
where to start.

> Yes, of course, your code does exactly that without any bytecode hacks.
>

Steve Holden

2/13/2010 1:14:00 PM

0

Mark Lawrence wrote:
> Gabriel Genellina wrote:
>> En Fri, 12 Feb 2010 04:29:12 -0300, Arnaud Delobelle
>> <arnodel@googlemail.com> escribió:
>>
>>> I posted an example of a decorator that does just this in this thread a
>>> couple of days ago:
>>>
>>> http://mail.python.org/pipermail/python-list/2010-February/12...
>>
>> Ouch! I didn't see your post, nor several other earlier posts in this
>> thread. In fact, I thought mine was the *first* reply when I wrote it!
>
> It's not just me then. I'm using Thunderbird on Windows Vista to view
> the group via gmane, and notice the earlier parts of a thread can show
> up hours or even days after the later ones. Would something who
> understands these things be kind enough to explain why this happens, or
> at least provide the keywords needed for google, as I haven't got a clue
> where to start.
>
There are two main channels feeding the comp.lang.python newsgroup.
Gmane is primarily a news (NNTP-based) service, though for reasons best
know to the organizers they choose to rename the standard newsgroups to
fit their own idea of how the universe should be organized, so there the
group becomes gmane.comp.python.general.

The first feed is the general interchange of articles between NNTP
servers: while this is usually a reliable interchange mechanism, an
individual server can "glitch" occasionally (by losing network
connectivity, for example, or when some part of its news support
mechanism breaks temporarily).

The second is the python-list mailing list, maintained as a part of the
python.org infrastructure (by a hard-working team of volunteers, I
should add, who get little enough credit for the sterling work they do
day in day out to keep the real information flowing and the spam out).

More recently the waters have been muddied by Google Groups, which sadly
seems at time to be so badly managed (or simply not managed?) as to make
it a principal source of spam. This surprises me, as Gmail appears to
have very effective spam filtering. Many people appear to use Google
Groups as their primary interface to newsgroups and/or mailing lists to
the extent that they believe the Groups interface to be what everyone
uses to access the list.

The task of maintaining a two-way bridge between the comp.lang.python
NNTP community and the python-list email community while filtering out
the spam and ensuring that posts aren't duplicated too often is a
difficult one. For example, I hit "reply all" to your message, and had I
not trimmed the headers my NNTP client (Thunderbird) would have sent
copies to you, to the mailing list AND to the newsgroup. Sometimes such
messages do appear twice, particularly (in my experience) to recipients
who use the email channel.

In short, these things happen because life is complicated and the
technical solutions available to disambiguate the feeds aren't perfect.
So you might consider filing the glitches under "shit happens". Speaking
personally I wouldn't have a clue how to keep this stuff going on a
daily basis at the volume level we see on this group, and I am very
grateful to everyone who does.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...

Mark Lawrence

2/13/2010 1:24:00 PM

0

Steve Holden wrote:
> Mark Lawrence wrote:
>> Gabriel Genellina wrote:
>>> En Fri, 12 Feb 2010 04:29:12 -0300, Arnaud Delobelle
>>> <arnodel@googlemail.com> escribió:
>>>
>>>> I posted an example of a decorator that does just this in this thread a
>>>> couple of days ago:
>>>>
>>>> http://mail.python.org/pipermail/python-list/2010-February/12...
>>> Ouch! I didn't see your post, nor several other earlier posts in this
>>> thread. In fact, I thought mine was the *first* reply when I wrote it!
>> It's not just me then. I'm using Thunderbird on Windows Vista to view
>> the group via gmane, and notice the earlier parts of a thread can show
>> up hours or even days after the later ones. Would something who
>> understands these things be kind enough to explain why this happens, or
>> at least provide the keywords needed for google, as I haven't got a clue
>> where to start.
>>
> There are two main channels feeding the comp.lang.python newsgroup.
> Gmane is primarily a news (NNTP-based) service, though for reasons best
> know to the organizers they choose to rename the standard newsgroups to
> fit their own idea of how the universe should be organized, so there the
> group becomes gmane.comp.python.general.
>
> The first feed is the general interchange of articles between NNTP
> servers: while this is usually a reliable interchange mechanism, an
> individual server can "glitch" occasionally (by losing network
> connectivity, for example, or when some part of its news support
> mechanism breaks temporarily).
>
> The second is the python-list mailing list, maintained as a part of the
> python.org infrastructure (by a hard-working team of volunteers, I
> should add, who get little enough credit for the sterling work they do
> day in day out to keep the real information flowing and the spam out).
>
> More recently the waters have been muddied by Google Groups, which sadly
> seems at time to be so badly managed (or simply not managed?) as to make
> it a principal source of spam. This surprises me, as Gmail appears to
> have very effective spam filtering. Many people appear to use Google
> Groups as their primary interface to newsgroups and/or mailing lists to
> the extent that they believe the Groups interface to be what everyone
> uses to access the list.
>
> The task of maintaining a two-way bridge between the comp.lang.python
> NNTP community and the python-list email community while filtering out
> the spam and ensuring that posts aren't duplicated too often is a
> difficult one. For example, I hit "reply all" to your message, and had I
> not trimmed the headers my NNTP client (Thunderbird) would have sent
> copies to you, to the mailing list AND to the newsgroup. Sometimes such
> messages do appear twice, particularly (in my experience) to recipients
> who use the email channel.
>
> In short, these things happen because life is complicated and the
> technical solutions available to disambiguate the feeds aren't perfect.
> So you might consider filing the glitches under "shit happens". Speaking
> personally I wouldn't have a clue how to keep this stuff going on a
> daily basis at the volume level we see on this group, and I am very
> grateful to everyone who does.
>
> regards
> Steve

Many thanks for the explanation.

Kindest regards.

Mark Lawrence

Terry Reedy

2/14/2010 9:24:00 PM

0

On 2/13/2010 8:14 AM, Steve Holden wrote:

> Gmane is primarily a news (NNTP-based) service, though for reasons best
> know to the organizers they choose to rename the standard newsgroups to
> fit their own idea of how the universe should be organized, so there the
> group becomes gmane.comp.python.general.

As I understand the site, the last I read a year or two ago, gmane is a
technical mailinglist-to-news gateway. It does not mirror newsgroups,
standard or not. It only mirrors lists such as python-list, not
newsgroups, such comp.lang.python. In doing so, it organizes the lists
into categories like g.comp.python, which has about 200 subgroups
mirroring 200 python lists.

Steve Holden

2/14/2010 9:33:00 PM

0

Terry Reedy wrote:
> On 2/13/2010 8:14 AM, Steve Holden wrote:
>
>> Gmane is primarily a news (NNTP-based) service, though for reasons best
>> know to the organizers they choose to rename the standard newsgroups to
>> fit their own idea of how the universe should be organized, so there the
>> group becomes gmane.comp.python.general.
>
> As I understand the site, the last I read a year or two ago, gmane is a
> technical mailinglist-to-news gateway. It does not mirror newsgroups,
> standard or not. It only mirrors lists such as python-list, not
> newsgroups, such comp.lang.python. In doing so, it organizes the lists
> into categories like g.comp.python, which has about 200 subgroups
> mirroring 200 python lists.
>
That would make more sense. I know that the python.org infrastructure
acts as a gateway between the mailing list and the newsgroup.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us....
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...