实例变量的类型
假设我有一个这样的类,
class Employee:
pass
我为 Employee
创建两个对象,如下所示
john = Employee()
rob = Employee()
..并创建实例变量
john.age = 12
rob.age = '15'
编译器接受这两个对象并打印年龄(约翰的年龄为 int
和rob 的年龄(字符串形式)。这怎么逻辑呢?每个对象中具有不同类型的相同数据属性。
谢谢。
Suppose that I have a class like this
class Employee:
pass
I create two objects for Employee
as below
john = Employee()
rob = Employee()
..and create instance variables
john.age = 12
rob.age = '15'
The compiler accepts both and prints the age (john's age in int
and rob's age in string
). How is this logical? The same data attribute having different type in each object.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
因为通过说
rob.age
,您没有创建具有特定类型的类范围数据属性;您只是创建一个实例本地的、特定于实例的属性,该属性引用一个具体实体,即字符串'15'
。要创建类范围的属性,您必须说Employee.age = ...
或在class Employee:
块内设置age
。通过将属性设置为描述符,我想您可以在每次设置时检查其类型并将其限制为整数或字符串或其他任何内容;但一般来说,类属性或实例属性都只是对象的名称,Python 关心的只是.age
命名对象。请注意,Python 无论如何都无法真正猜出你的意思。如果您说 john.age 是 12,那么您似乎希望 Python 猜测所有其他 .age 属性也应该是数字。但为什么 Python 不应该更进一步,猜测它们是整数——或者更好的是,它们是正偶数呢?我真的认为在任何情况下,Python 从单个赋值推断出某种猜测,以了解如何在类的所有其他实例中处理该属性,都是不合理的。
Because by saying
rob.age
you are not creating a class-wide data attribute that has a specific type; you are merely creating an instance-local, instance-specific attribute that refers to a concrete entity, the string'15'
. To create a class-wide attribute you would have to sayEmployee.age = …
or setage
inside theclass Employee:
block. By setting the attribute to a descriptor, I suppose you could check its type every time it is set and restrict it to an integer or string or whatever; but in general, either a class attribute or an instance attribute is just a name for an object, and all Python cares is that.age
names an object.And note that Python could not really guess what you mean anyway. If you say that
john.age
is 12, you seem to want Python to guess that all other.age
attributes should also be numbers. But why shouldn't Python go even further, and guess that they are integers — or better yet, that they are positive even integers? I really do not think it would be reasonable in any case for Python to extrapolate from a single assignment to some kind of guess as to how you will treat that attribute in all other instances of the class.从根本上来说,这就是当您拥有动态类型语言时所得到的。
类型是在运行时而不是声明时确定的。
它有优点也有缺点,但我相信在大多数开发环境中优点大于缺点。
(Ruby、Python、PHP 等都是这么做的)
It's fundamentally what you get when you have a dynamically typed language.
Type is determined at runtime not at declaration.
It has advantages and disadvantages, but I believe the advantages outweigh its disadvantages in most development contexts.
(It's what Ruby, Python, PHP, etc. do)
Python 编译器不关心也不需要关心您将什么类型的值绑定到属性/名称; Python 的动态特性意味着重要的类型检查(通常实际上是属性检查)是在运行时完成的。
Python's compiler does not care what type of value you bind to an attribute/name, nor does it have to; Python's dynamic nature means that the important type checks (which are usually actually attribute checks) are done at runtime.
术语“变量”在 Python 中很容易混淆。
The term "variable" is confusioning in Python.
请务必理解这个基本原则:在 Python 中,变量没有类型。 值有类型。这是 Python 作为动态类型语言的本质,类似于 Lisp 和 Javascript,但与 C、C++ 和 Java 不同。
以下是 Wikipedia 有关 Python 中输入的条目的摘录:
如果您想学习 Python,请阅读有关此主题的更多内容(尤其是 Duck Typing 是什么)。
PS 这个问题与对象的属性完全正交。属性只是 Python 中的其他“变量”,它也可以保存值。这些值可以是不同的类型。
Be sure to understand this fundamental principle: in Python, variables don't have types. Values have types. This is the essence of Python's being a dynamically-typed language similarly to Lisp and Javascript, but unlike C, C++ and Java.
Here's an excerpt from Wikipedia's entry on typing in Python:
Do read more on this subject (especially what Duck Typing is) if you want to learn Python.
P.S. This issue is totally orthogonal to attributes of objects. Attributes are just other "variables" in Python, which also can hold values. These values can be of different types.