程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

8、 Python learning notes - object oriented -metaclass

編輯:Python
# metaclass Is the class used to create the class
"""
1、 All things are objects ,python Of class( class ) It's also an object ( example ), It is tpye The object of ( example )
2、 All classes inherit by default object
3、 The essence of creating classes is type Class instantiation class = tpye(), So for type Category self Is the class name.
"""
# Example 1、 Demonstrates the nature of creating classes
class Foo1:
def bar(self):
print('hello Foo1')
# The following expression is completely equivalent to the above
"""
1、 All classes are tpye The object of , So creating a class is actually creating type The object of ,object Is the default parent of all classes ( Even if it is not declared when it is created )
2、{'bar': fun} Show class Foo2 There's a function in bar, The corresponding function is fun
"""
def fun(self):
print('hello Foo2')
Foo2 = type('Foo2', (object,), {'bar': fun})
foo1 = Foo1()
foo2 = Foo2()
foo1.bar()
foo2.bar()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved