Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Storing objects required by functions.
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
 
Christos "TZOTZIOY" Georgiou  
View profile  
 More options Dec 30 2003, 9:04 pm
Newsgroups: comp.lang.python
From: Christos "TZOTZIOY" Georgiou <t...@sil-tec.gr>
Date: Wed, 31 Dec 2003 04:04:47 +0200
Local: Tues, Dec 30 2003 9:04 pm
Subject: Re: Storing objects required by functions.
On 31 Dec 2003 00:08:47 GMT, rumours say that b...@oz.net (Bengt
Richter) might have written:

>The other way is to take advantage of functions' roles as decriptors and the mechanism that
>makes bound methods with a self as the first arg, but the rest apparently normal. I.e,
>we can put the r parameter in the place of self (not specifically tested)

>    def uses_self(r, x):
>        pass
>    uses_self = uses_self.__get__(re.compile("..."))

It's much more general to use new.instancemethod.  See:

def voodoo(function, *its_arguments):
    from new import instancemethod
    def child(function, first_argument, *rest_of_arguments):
        if rest_of_arguments:
            return child(
                instancemethod(function, first_argument, object),
                *rest_of_arguments
            )
        else:
            return instancemethod(function, first_argument, object)
    return child(function, *its_arguments)

The import statement is in the voodoo just for completeness including it
here.
The function above recurses in order to allow stuff like:

getter = voodoo(getattr, my_object, "its_attribute")

or the more modern

getter = voodoo(operator.itemgetter("its_attribute"), my_object)

and similarly

setter = voodoo(operator, my_object, "its_attribute")

allowing

setter(value)

at good speeds.

I have a module predicates.py defining All and Any classes for
iterables, and the trick above plus itertools allows *some* operations
to run faster than correspondent python code...
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix


    Reply to author    Forward  
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.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google