>>> class CObj(object):
... pass
...
>>> dir()
['CObj', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> cob = CObj
>>> dir()
['CObj', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'cob']

>>> help(__name__)
Help on module __main__:

NAME
__main__

CLASSES
builtins.object
CObj

class CObj(builtins.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

cob = class CObj(builtins.object)
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

FILE
(built-in)

>>> cins = cob()
>>> cins
<__main__.CObj object at 0x0000000000B5CC88>
>>> cins2 = CObj()
>>> cins2
<__main__.CObj object at 0x00000000013F5E10>
>>> CObj.x1 = 20
>>> cins
<__main__.CObj object at 0x0000000000B5CC88>
>>> cins.x1
20
>>> cins2.x1
20
>>> cins2.y1=30
>>> cins.y1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'CObj' object has no attribute 'y1'
>>> CObj.y1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'CObj' has no attribute 'y1'
>>> dir(cins)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__
gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__
repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x1']
>>> dir(cins2)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__
gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__
repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x1', 'y1']
>>> dir(cob)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__
gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__
repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x1']
>>> dir(CObj)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__
gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__
repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x1']
>>>

>>> hasattr(CObj, x1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x1' is not defined
>>> hasattr(CObj, 'x1')
True
>>> hasattr(CObj, 'y1')
False
>>> hasattr(CObj, 'y1')

python class metaclass instance的更多相关文章

  1. Python魔法 - MetaClass

    Python魔法 - MetaClass metaclass The class of a class. Class definitions create a class name, a class ...

  2. python's metaclass

    [python's metaclass] 和objc中类似,metaclass用于创建一个类对象,但与objc不同的是,objc中每个类对象有各自不同的metaclass,而python中的metac ...

  3. Python中metaclass解释

    Classes as objects 首先,在认识metaclass之前,你需要认识下python中的class.python中class的奇怪特性借鉴了smalltalk语言.大多数语言中,clas ...

  4. Python进阶:metaclass谈

    metaclass 的超越变形特性有什么用? 来看yaml的实例: import yaml class Monster(yaml.YAMLObject): yaml_tag = u'!Monster' ...

  5. python的metaclass

    元类一般用于创建类.在执行类定义时,解释器必须要知道这个类的正确的元类.解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属性赋值给此类作为它的元类.如果此属性没有定义,它会向上 ...

  6. Python之Metaclass详解,Python之元类

    本人Java程序员一枚,这几天闲来无事就自学了下Python,学到Metaclass感觉有点迷惑,就在网上查相关资料,在栈溢出(stackoverflow)网站上看到一个关于metaclass的回答, ...

  7. python中Metaclass的理解

    今天在学习<python3爬虫开发实战>中看到这样一段代码3 class ProxyMetaclass(type): def __new__(cls, name, bases, attrs ...

  8. python type metaclass

    在python中一切皆对象, 所有类的鼻祖都是type, 也就是所有类都是通过type来创建. 传统创建类 class Foo(object): def __init__(self,name): se ...

  9. python的metaclass浅析-乾颐堂

    元类一般用于创建类.在执行类定义时,解释器必须要知道这个类的正确的元类.解释器会先寻找类属性__metaclass__,如果此属性存在,就将这个属性赋值给此类作为它的元类.如果此属性没有定义,它会向上 ...

随机推荐

  1. 【WPF】值是枚举的RadioButton 绑定问题

    源 1.RadioButton 2.IValueConverter 3.枚举 xaml实现 <RadioButton Content="单打热身" GroupName=&qu ...

  2. Cookie的应用

    作用:在浏览器当中用cookie来保存参数,比如实现登录功能时用来保存账号 <%@ page language="java" import="java.util.* ...

  3. 用上了ReSharper

    用上了ReSharper,发现很爽1.使用var2.去掉无用的using3.将foreach改为linq let4.字符串为cost5.作用域与变量名适配6.文件夹与namespace不匹配7.去掉为 ...

  4. jQuery Mobile入门

    转:http://www.cnblogs.com/linjiqin/archive/2011/07/17/2108896.html 简介:jQuery Mobile框架可以轻松的帮助我们实现非常好看的 ...

  5. 罗马数字转整数Leetcode13

    该题较为简单,但是需要知道罗马数字的表示以及取值.用了一下map,其实之前没用过,但仔细看了一下跟python的字典实际上差不多,扫了一遍函数就直接可以用了. class Solution { pub ...

  6. 日历插件FullCalendar应用:(二)数据增删改

    接上一篇 日历插件FullCalendar应用:(一)数据展现. 这一篇主要讲使用fullcalendar插件如何做数据的增删改,用到了art.dialog web对话框组件,上一篇用到的webFor ...

  7. jdk 环境变量配置

    环境变量:Path %JAVA_HOME%\bin;%JAVA_HOME%\jre\binCLASSPATH .;%JAVA_HOME%\lib;JAVA_HOME D:\java\jdk1.5.0_ ...

  8. [转] C++的引用传递、指针传递参数在java中的相应处理方法

    原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...

  9. Scrum meeting 记录

    本周Scrum Master 侯宇泰 一. 工作完成内容记录 · 陈双: 1. 后端数据库Azure Storage设计 2. Offline 上传Movie信息 3. 主页和配音页面的连接 · 鲍航 ...

  10. Linux终端使用代理服务器

    1.YUM添加代理服务器: [root@localhost /]# vim /etc/yum.conf 添加如下项目: proxy=http://172.16.1.188:8888/2.WGET添加代 ...