实例变量的类型

发布于 2024-10-25 17:15:07 字数 351 浏览 2 评论 0原文

假设我有一个这样的类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

世界如花海般美丽 2024-11-01 17:15:08

因为通过说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 say Employee.age = … or set age inside the class 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.

往事风中埋 2024-11-01 17:15:08

从根本上来说,这就是当您拥有动态类型语言时所得到的。

类型是在运行时而不是声明时确定的。

它有优点也有缺点,但我相信在大多数开发环境中优点大于缺点。

(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)

半窗疏影 2024-11-01 17:15:08

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.

青春有你 2024-11-01 17:15:08

术语“变量”在 Python 中很容易混淆。

The term "variable" is confusioning in Python.

神经暖 2024-11-01 17:15:07

请务必理解这个基本原则:在 Python 中,变量没有类型。 有类型。这是 Python 作为动态类型语言的本质,类似于 Lisp 和 Javascript,但与 C、C++ 和 Java 不同。

>>> foo = 5       # foo now holds a value of type int
>>> foo = 'hello' # foo now holds a value of type string

以下是 Wikipedia 有关 Python 中输入的条目的摘录:

Python 使用鸭子类型并已输入
对象但无类型变量名。
不检查类型约束
编译时间;相反,操作
对象可能会失败,这意味着
给定的对象不适合
类型。尽管是动态类型的,
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.

>>> foo = 5       # foo now holds a value of type int
>>> foo = 'hello' # foo now holds a value of type string

Here's an excerpt from Wikipedia's entry on typing in Python:

Python uses duck typing and has typed
objects but untyped variable names.
Type constraints are not checked at
compile time; rather, operations on an
object may fail, signifying that the
given object is not of a suitable
type. Despite being dynamically typed,
Python is strongly typed, forbidding
operations that are not well-defined
(for example, adding a number to a
string) rather than silently
attempting to make sense of them.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文