addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwchatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrosseditemptyheartexportfacebookfolderfullheartglobegmailgoogleimagesinstagramlinklocation-pinmagnifying-glassmailminusmoremuplabelShape 3 + Rectangle 1outlookpersonplusprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

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

From: Thomas E J.
Sent on: Thursday, January 12, 2012 12:16 PM

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
    def blah(cls):
        return +

    duh = True
    def argh(cls):
        if cls.duh:
            return cls.blah()
            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)

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


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