From eb7a89c4400cdb3fa7fe09289e10a3159a5e2c57 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Wed, 16 May 2018 23:18:00 +0530 Subject: Adding ipyml files for the demo code. --- advanced_python/code/decorators.ipyml | 73 ++++++++++++++++++++++ advanced_python/code/multiple_inheritance.ipyml | 83 +++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 advanced_python/code/decorators.ipyml create mode 100644 advanced_python/code/multiple_inheritance.ipyml (limited to 'advanced_python') diff --git a/advanced_python/code/decorators.ipyml b/advanced_python/code/decorators.ipyml new file mode 100644 index 0000000..4907d0c --- /dev/null +++ b/advanced_python/code/decorators.ipyml @@ -0,0 +1,73 @@ +cells: + +- markdown: | + # Simplest decorator + +- code: | + + def deco(func): + #print("deco") + return func + + @deco + def greet(): + print("Namaste!") + + +- code: | + greet() + + +- markdown: | + # Using wraps + +- code: | + + from functools import wraps + + def deco(func): + @wraps(func) + def new_func(*args, **kw): + print("Hello") + return func(*args, **kw) + return new_func + + @deco + def greet(): + '''Print greeting.''' + print("Namaste!") + +- code: | + greet() + + +- markdown: | + # Decorator taking arguments + +- code: | + + from functools import wraps + + def deco(func=None, greet='Hello'): + def wrapper(func): + @wraps(func) + def new_func(*args, **kw): + print(greet) + return func(*args, **kw) + return new_func + if func is None: + return wrapper + else: + return wrapper(func) + + @deco + def f(): + print('Hi') + + @deco(greet='Namaste') + def g(): + print('Hi') + +- code: | + f() + g() diff --git a/advanced_python/code/multiple_inheritance.ipyml b/advanced_python/code/multiple_inheritance.ipyml new file mode 100644 index 0000000..762c5e0 --- /dev/null +++ b/advanced_python/code/multiple_inheritance.ipyml @@ -0,0 +1,83 @@ +cells: + +- markdown: | + # First example + +- code: | + + class Animal: + def eat(self): + pass + + class Mammal(Animal): + def hair_color(self): + pass + + class FlyingAnimal(Animal): + def fly(self): + pass + + class Bat(Mammal, FlyingAnimal): + pass + +- code: | + + b = Bat() + b.eat() + b.hair_color() + b.fly() + +- markdown: | + # Example 2 + +- code: | + + class Base: + def __init__(self): + print('Base') + + class A(Base): + def __init__(self): + print('A') + super().__init__() + + class B(Base): + def __init__(self): + print('B') + super().__init__() + + class C(A, B): + def __init__(self): + print('C') + super().__init__() + + class C1(B, A): + def __init__(self): + print('C1') + super().__init__() + + +- code: | + c = C() + +- code: | + c1 = C1() + +- markdown: | + + # Example 3 + +- code: | + + class A: + def __init__(self): + print('A') + + class B: + def __init__(self): + print('B') + + class C(B, A): + def __init__(self): + print('C') + super().__init__() -- cgit