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

From: Erik O.
Sent on: Thursday, January 12, 2012 12:05 PM
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

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