python type class object关系

a=1

type(a) ##<class 'int'>

int.__bases__##(<class 'object'>,)

type(int)##<class 'type'>

type(object)##<class 'type'>

object.__bases__##()

type(object)##type(a) type(dict)

tpye(name, bases, dict)

  • name 类名
  • bases 父类的元组
  • dict 类的属性方法和值组成的键值对
  • # 实例方法
    def instancetest(self):
        print("this is a instance method")
    
    
    # 类方法
    @classmethod
    def classtest(cls):
        print("this is a class method")
    
    
    # 静态方法
    @staticmethod
    def statictest():
        print("this is a static method")
    
    
    # 创建类
    test_property = {"name": "tom", "instancetest": instancetest, "classtest": classtest, "statictest": statictest}
    Test = type("Test", (), test_property)
    
    
    # 创建对象
    test = Test()
    # 调用方法
    print(test.name)
    test.instancetest()
    test.classtest()
    test.statictest()

     

 

posted @ 2019-11-27 11:14  howhy  阅读(136)  评论(0)    收藏  举报