CSC326 Lecture 9 - CSC326 Meta Programming i CSC326 Meta...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSC326 Meta Programming i CSC326 Meta Programming CSC326 Meta Programming ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ CSC326 Meta Programming iii Contents 1 Agenda 1 2 Class Factory 1 3 Meta Class 1 4 Decorator 2 5 Misuse of Decorators 3 6 Using Decorators 4 7 Aspect Oriented Programming 5 8 Decorator Tool 5 9 Usage in Bottle 6 10 Recap 6 CSC326 Meta Programming 1 / 6 1 Agenda Python Types and Objects Meta Classes Decorators Aspect-oriented Programming 2 Class Factory Go through Shalabh Chaturvedis tutorial 3 Meta Class Class factory Class that acts as template for other classes Old fashioned: a function >>> def class_with_method(func): ... class klass: pass ... setattr(klass, func.__name__, func) ... return klass ... >>> def say_foo(self): print foo ... >>> Foo = class_with_method(say_foo) >>> foo = Foo() >>> foo.say_foo() foo Another way #----- Class factory in the [new] module (python) -------# >>> from new import classobj >>> Foo2 = classobj(Foo2,(Foo,),{bar:lambda self:bar}) >>> Foo2().bar() bar >>> Foo2().say_foo() foo Use type special class type is just a class factory: same as new.classobj >>> X = type(X,(),{foo:lambda self:foo}) >>> X, X().foo() (<class __main__.X>, foo) Inherit from type CSC326 Meta Programming 2 / 6 >>> class ChattyType(type): ... def __new__(cls, name, bases, dct): ... print "Allocating memory for class", name ... return type.__new__(cls, name, bases, dct) ... def __init__(cls, name, bases, dct): ... print "Initing (configuring) class", name...
View Full Document

Page1 / 9

CSC326 Lecture 9 - CSC326 Meta Programming i CSC326 Meta...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online