GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LearnPython/python_metaclass.py at master · oooou/LearnPython Expand file tree/ Copy pathpython_metaclass.pyMore file actions 55 lines (39 loc) · 1.17 KB / Copy pathpython_metaclass.pyFile metadata and controls55 lines (39 loc) · 1.17 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 # _*_ coding: utf-8 _*_ """ python_metaclass.py by xianhu """ class Foo(object): def hello(self): print("hello world!") return foo = Foo() print(type(foo)) # <class '__main__.Foo'> print(type(foo.hello)) # <class 'method'> print(type(Foo)) # <class 'type'> temp = Foo # 赋值给其他变量 Foo.var = 11 # 增加参数 print(Foo) # 作为函数参数 # ======================================================================== def init(self, name): self.name = name return def hello(self): print("hello %s" % self.name) return Foo = type("Foo", (object,), {"__init__": init, "hello": hello, "cls_var": 10}) foo = Foo("xianhu") print(foo.hello()) print(Foo.cls_var) print(foo.__class__) print(Foo.__class__) print(type.__class__) # ======================================================================== class Author(type): def __new__(mcs, name, bases, dict): # 添加作者属性 dict["author"] = "xianhu" return super(Author, mcs).__new__(mcs, name, bases, dict) class Foo(object, metaclass=Author): pass foo = Foo() print(foo.author) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |