好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python元类实例解析_python

这篇文章主要介绍了 Python 元类实例解析,此文的主要任务就是给大家彻底讲明白什么是元类,需要的朋友可以参考下

>>> i = 10
>>> s = "abc"
>>> nums = [1,2,3]
>>> dicts = {"name":"zhang"} 
>>> id(i)
40592592
>>> id(s)
44980584 
>>> type(i)
<class 'int'>
>>> type(s)
<class 'str'>
>>> type(nums)
<class 'list'>
>>> type(dicts)
<class 'dict'> 
>>> class Person:
  # 这里的 self 指某个实例对象自己
...  def __init__(self, name):
   # name 是实例的属性
...   self.name = name
  # live 是类的属性
  live = True 
>>> p1 = Person("zhangsan")
>>> p1.name
'zhangsan'
>>>
>>> p2 = Person("lisi")
>>> p2.name
'lisi' 
>>> p1
<__main__.Person object at 0x0195AA30>
>>> type(p1)
<class '__main__.Person'> # 这里的__main__是模块名称 
>>> id(Person)
26564024
>>> type(Person)
<class 'type'>
>>> Person
<class '__main__.Person'> 
>>> nums = [1,2,3]
>>> type(nums)
<class 'list'>
>>> type(list)
<class 'type'> 
>>> Person = type("Person", (), {"live":True})
>>> Person
<class '__main__.Person'> 
>>> class Person:
...  live = True
...
>>> Person
<class '__main__.Person'> 

相关推荐:

Python解析JSON的实例分享

分析Python解析XML的几种方式

以上就是Python 元类实例解析_python的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python元类实例解析_python的详细内容...

  阅读:41次