Python 类中实例变量和类变量还有类内定义的函数,它们之间作用域是怎么样的,变量之间的关系是怎么样
你去看官方文档不行啊 :-(类内定义的函数(即该类的方法)和「类变量」位于同一命名空间。实例变量访问时是这个样子的:访问时,如果该实例拥有该属性,则返回之,否则查找并返回该实例所属类的该属性;赋值时,如果该实例没有该属性,则创建之,否则更新之;删除时,在没有覆盖 __delattr__ 的时候,如果该实例有该属性则删除之,否则报错;特殊方法(__init__、__del__、__len__ 等)的间接调用,仅使用类属性,不使用实例属性。
■网友
谢邀。首先,Python并没有对“实例变量”或者“类变量”进行区分,而是通过user-defined attribute与attribute accessing protocal整合这两个概念。Python Attributes and Methods中是这么写的:When accessed (for e.g. printobjectname.attributename), the following objects aresearched in sequence for the attribute:
The object itself(objectname.__dict__ or anyPython-provided attribute ofobjectname).
The object\u0026#39;s type(objectname.__class__.__dict__). Observe that only__dict__ is searched, which means onlyuser-provided attributes of the class. In otherwords objectname.__bases__ may not return anythingeven though objectname.__class__.__bases__ doesexist.
The bases of the object\u0026#39;s class, their bases, and soon. (__dict__ of each ofobjectname.__class__.__bases__). More than one basedoes not confuse Python, and should not concern us at the moment. Thepoint to note is that all bases are searched until an attribute isfound.
If all this hunting around fails to find a suitably named attribute,Python raises an AttributeError. The type of thetype (objectname.__class__.__class__) is neversearched for attribute access on an object(objectname in the example).
The built-in dir() function returns a list ofall attributes of an object. Also look atthe inspectmodule in the standard library for more functions to inspectobjects.
The above section explains the general mechanism forall objects. Even for classes (for exampleaccessing classname.attrname), with a slightmodification: the bases of the class are searched before theclass of the class (which isclassname.__class__ and for most types, by theway, is \u0026lt;type \u0026#39;type\u0026#39;\u0026gt;).
其次,关于class中定义的function的scope,文档中(Data model)有如下说明(当然这里说的是new-style class):The class body is executed (approximately) asexec(body, globals(), namespace). The key difference from a normalcall to exec() is that lexical scoping allows the class body (includingany methods) to reference names from the current and outer scopes when theclass definition occurs inside a function.
However, even when the class definition occurs inside the function, methodsdefined inside the class still cannot see names defined at the class scope.Class variables must be accessed through the first parameter of instance orclass methods, and cannot be accessed at all from static methods.
下面是较为“高级”的内容:Execution model:...The scope of names defined in a class block is limited to theclass block; it does not extend to the code blocks of methods – this includescomprehensions and generator expressions since they are implemented using afunction scope. This means that the following will fail:
推荐阅读
- 怎样成为一名合格的Python程序员?
- python 爬虫,咋获得输入验证码之后的搜索结果
- python的html5lib这个库咋使用啊我在网上也没有找到相关文档
- 零基础入门学习啥语言好
- Python3.4和3.5区别大么
- python 中 def_():...... return _有啥作用
- 新互联网网站用Java还靠谱么对比Php,Python,Ruby的话
- 30岁男,创业失败转行学python,是否很晚?也不好找工作?
- Python 的开发速度比 C#.net 或 Vb.net 更快吗?
- 16年毕业,建筑环境与设备工程专业,干的一直是工程想转IT专业,想从python入手请问咋开始学习
