Skip to content

Instantly share code, notes, and snippets.

@ohta-rh
Created July 17, 2012 08:07
Show Gist options
  • Select an option

  • Save ohta-rh/3127914 to your computer and use it in GitHub Desktop.

Select an option

Save ohta-rh/3127914 to your computer and use it in GitHub Desktop.
[hello python!]Defined class method and class variable
#coding: utf-8
class Hoge
strategy = None
@classmethod
def set_strategy(cls, strategy) :
self.strategy = strategy || Piyo
@classmethod
def say(cls) :
return self.strategy.say()
class Piyo
def say(self) :
print "piyo"
class Fuga
def say(self) :
print "fuga"
#-==-=--=-=--==
Hoge.say()
=>piyo
Hoge.set_stragery(Fuga)
Hoge.say()
=>fuga
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment