Re: [PhillyPUG] Python hackery, any better ideas?

From: Dustin G.
Sent on: Thursday, January 12, 2012 12:27 PM
mat,

the gevent library does some funky scope stuff when it monkey patches the sockets module etc. maybe it will give you some ideas. see first 130 lines:

https://bitbucket.org/denis/gevent/src/b045e5f368f4/gevent/socket.py

On Thu, Jan 12, 2012 at 12:21 PM, Thomas E Jenkins <[address removed]> wrote:


On Thu, Jan 12, 2012 at 12:05, Erik Osheim <[address removed]> wrote:
So, a good strategy for dealing with this is using classes as singleton objects. This way you can call the classmethods without having to create an instance, but you can create subclasses which overload particular member variables and/or methods.

For example:

class Foo(object):
    foo = 1
    bar = 2
    @classmethod
    def blah(cls):
        return cls.foo + cls.bar

    duh = True
    @classmethod
    def argh(cls):
        if cls.duh:
            return cls.blah()
        else:
            return None

class Bar(Foo):
    duh = False

class Duh(Foo):
    bar = 99

Foo.argh() -> 3
Bar.argh() -> None
Duh.arg() -> 100

Hope this helps

Along those same lines you can put all the vars in a class like above and just pass the class or an instance of it to your hairy function if you want to keep the actual code in a function instead of making it a class.

class Vars(object):
    frob = 42
    other = 23

def hairyfunction(vars):
    return vars.frob + vars.other

>> hairyfunction(Vars)
65

>> v = Vars()
>> v.frob = 0
>> hairyfunction(vars)
23

Thomas





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Thomas E Jenkins ([address removed]) from The Philadelphia Python Users Group (PhillyPUG).
To learn more about Thomas E Jenkins, visit his/her member profile

To unsubscribe or to update your mailing list settings, click here

Meetup, PO Box 4668 #37895 New York, New York[masked] | [address removed]

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy