Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindlychung/9804a5a929cf63bb59bd to your computer and use it in GitHub Desktop.
Save kindlychung/9804a5a929cf63bb59bd to your computer and use it in GitHub Desktop.
python oop, object oriented
class Animal:
__metaclass__ = ABCMeta
@abstractmethod
def say_something(self):
return "I'm an animal!"
class Cat(Animal):
def say_something(self):
s = super(Cat, self).say_something()
return "%s - %s" % (s, "Miauuu")
# PythonDecorators/decorator_without_arguments.py
class decorator_without_arguments(object):
def __init__(self, decorated):
"""
If there are no decorator arguments, the function
to be decorated is passed to the constructor.
"""
print("Inside __init__()")
self.decorated = decorated
def __call__(self, *args):
"""
__call__ is the wrapper
The __call__ method is not called until the
decorated function is called.
"""
print("Inside __call__()")
self.decorated(*args)
print("After self.f(*args)")
@decorator_without_arguments
def sayHello(a1, a2, a3, a4):
print('sayHello arguments:', a1, a2, a3, a4)
# First call creates a new decorator instance
sayHello("say", "hello", "argument", "list")
# Second call uses the __call__ method of that instance
sayHello("say", "hello", "argument", "list1")
# Decoration is equivalent to this:
def sayHello(a1, a2, a3, a4):
print('sayHello arguments:', a1, a2, a3, a4)
sayHello = decorator_without_arguments(sayHello)
sayHello("say", "hello", "argument", "list")
sayHello("say", "hello", "argument", "list1")
##################################################################
# PythonDecorators/decorator_with_arguments.py
class decorator_with_arguments(object):
def __init__(self, arg1, arg2, arg3):
"""
If there are decorator arguments, the function
to be decorated is not passed to the constructor!
"""
print("Inside __init__()")
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
def __call__(self, f):
"""
__call__ returns a wrapper
If there are decorator arguments, __call__() is only called
once, as part of the decoration process! You can only give
it a single argument, which is the function object.
"""
print("Inside __call__()")
def wrapped_f(*args):
print("Inside wrapped_f()")
print("Decorator arguments:", self.arg1, self.arg2, self.arg3)
f(*args)
print("After f(*args)")
return wrapped_f
@decorator_with_arguments("hello", "world", 42)
def sayHello(a1, a2, a3, a4):
print('sayHello arguments:', a1, a2, a3, a4)
sayHello("say", "hello", "argument", "list")
sayHello("a", "different", "set of", "arguments")
# Decoration is equivalent to this:
def sayHello(a1, a2, a3, a4):
print('sayHello arguments:', a1, a2, a3, a4)
sayHello = decorator_with_arguments("hello", "world", "42")(sayHello)
sayHello("say", "hello", "arg", "list")
sayHello("say", "hello", "arg", "list1")
########################################################################
# Observer pattern
class Signal:
def __init__(self):
"""
:type self: Signal
:return:
"""
self.__subscribers = []
""":type: list of [function]"""
def emit(self, *args, **kwargs):
for sub in self.__subscribers:
sub(*args, **kwargs)
def connect(self, func):
self.__subscribers.append(func)
def disconnect(self, func):
try:
self.__subscribers.remove(func)
except ValueError:
print("Warning: function %s not removed from signal %s" % (func, self))
#############################################################
# strategy pattern
class StrategyExample :
def __init__(self, func=None) :
if func :
self.execute = func
def execute(self) :
print "Original execution"
def executeReplacement1() :
print "Strategy 1"
def executeReplacement2() :
print "Strategy 2"
if __name__ == "__main__" :
strat0 = StrategyExample()
strat1 = StrategyExample(executeReplacement1)
strat2 = StrategyExample(executeReplacement2)
strat0.execute()
strat1.execute()
strat2.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment