# class is also an object
# All Objects
class Person (object):
num = 0
Print ("--person--test--")
def __init__ (self):
Self.name = ' abc '
#--person--test--
Print (100)
# 100
Print ("Hello")
# Hello
Print ("TT")
# TT
Print (person)
# <class ' __main__. Person ' >
# Dynamic creation of classes
def choose_class (name):
If name = = ' Foo ':
Class Foo (object):
Pass
Return Foo #返回的是类, not an instance of class
Else
Class Bar (object):
Pass
Return Bar
MyClass = Choose_class (' foo ')
Print (MyClass) #函数返回的是类, not an instance of a class
# <class ' __main__.choose_class.<locals>. Foo ' >
Print (MyClass ()) #你可以通过这个类创建类实例, which is the object
# <__main__.choose_class.<locals>. Foo Object at 0x000001eedc675da0>
# Create an object with type
Print (type) #int
Print (Type ("12345")) #str
Class Person:
Pass
P1 = person ()
Print (Type (p1)) #<class ' __main__. Person ' >
Class Test:
Pass
T1 = Test ()
# Create class with type
# type ()
# type can accept a class that describes the seat parameter and then returns a class
# type (class name, a tuple of the parent class name (nullable for inheritance), containing the dictionary (name and value) of the property
Test2 = Type ("Test2", (), {})
Print (TEST2) # <class ' __main__. Test2 ' >
The # class is created with type
# The instance object is created by class, and class is created by type
def printnum (self):
Print ("--num--%d"%self.num)
Test3 = Type ("Test3", (), {"Printnum":p Rintnum})
T1 = Test3 ()
T1.num = 100
T1.printnum () #--num--100
Class Animal:
Def eat (self):
Print ('----eat-----')
Class Dog (Animal):
Pass
Wangcai = Dog ()
Wangcai.eat () #----Eat-----
Cat = Type ("Cat", (Animal,), {})
Xiaohuamao = Cat ()
Xiaohuamao.eat () #----Eat-----
Print (cat.__class__)
# <class ' type ' >
# What the hell is a meta-class?
# The Meta class is used to create these classes (objects), which are classes of classes
# MyClass = Metaclass () #使用元类创建处一个对象, this object becomes a class
# MyObject = MyClass () #使用 "class" to create an instance object
# __metaclass__ Property (in Python2)
# You can define a class again to add the __metaclass__ property to it
Class Too (object):
__metaclass__ = Something
# Custom meta-classes
def upper_attr (future_class_name,future_class_parents,future_class_attr):
Newattr = {}
For Name,vlaue in Future_class_attr.items ():
If not Name.startswitch ("__"): #以什么什么开头
Newattr[name.upprt ()] = value
return type (FUTURE_CLASS_NAME,FUTURE_CLASS_PARENTS,NEWATTR)
Class Too (object):
__metaclass__ = Upper_attr #设置Foo类的元类为uppper_attr
Bar = ' Bip '
Print (Hasattr (Foo, ' Bar '))
Print (Hasattr (Foo, ' BAR '))
t = Foo
Print (T.bar)
Print (T.bar)
# in Python3
def upper_attr (future_class_name,future_class_parents,future_class_attr):
Newattr = {}
For Name,vlaue in Future_class_attr.items ():
If not Name.startswitch ("__"): #以什么什么开头
Newattr[name.upprt ()] = value
return type (FUTURE_CLASS_NAME,FUTURE_CLASS_PARENTS,NEWATTR)
Class Too (Object,metaclass):
__metaclass__ = Upper_attr #设置Foo类的元类为uppper_attr
Bar = ' Bip '
Python meta-class