기본 콘텐츠로 건너뛰기

글

[python] hooking in python

source:  http://stackoverflow.com/questions/11635443/hooking-in-python Hook ing is a way to get your own code to execute when another system is running, whether that other system is an OS, a GUI, or whatever. A somewhat silly example in Python: def Process ( records , per_record_hook = None ): "adds all records to XYZ system" for record in records : if per_record_hook : per_record_hook ( record ) XYZ . append ( record ) def print_record ( record ): "print a '.' for each record (primitive counter)" print '.' and then later: Process ( records_from_somewhere , per_record_hook = print_record ) <RESULT> In [1]: records = [1,2,3] In [2]: XYZ=[] In [3]: %paste def Process(records, per_record_hook=None):     "adds all records to XYZ system"     for record in records:         if per_record_hook:             per_record_hook(record) ...

[python] What is a metaclass in Python?

source: http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python Classes as objects Before understanding metaclasses, you need to master classes in Python. And Python has a very peculiar idea of what classes are, borrowed from the Smalltalk language. In most languages, classes are just pieces of code that describe how to produce an object. That's kinda true in Python too: >>> class ObjectCreator ( object ): ... pass ... >>> my_object = ObjectCreator () >>> print ( my_object ) < __main__ . ObjectCreator object at 0x8974f2c > But classes are more than that in Python. Classes are objects too. Yes, objects. As soon as you use the keyword  class , Python executes it and creates an OBJECT. The instruction >>> class ObjectCreator ( object ): ... pass ... creates in memory an object with the name "ObjectCreator". This object (the class) is itself capable of creating objects...