简体   繁体   English

在 python 中,相同 class 的两个不同对象可以有不同的变量数

[英]In python can two different objects of same class have different no of variables

class car:
    pass

firstcar = car()
secondcar = car()

firstcar.wheels = 4
print(firstcar.wheels)

output: 4 output:4

But when I try print(type(secondcar.wheels)) it gives me AttributeError但是当我尝试print(type(secondcar.wheels))它给了我AttributeError

  1. When I create c1.wheels then why wheels is not created as an instance variable for object c2.当我创建c1.wheels时,为什么没有将轮子创建为 object c2 的实例变量。
  2. If wheels is not created for c2 then is it that object of same class can have different number of instance variables in python.如果没有为 c2 创建车轮,那么相同 class 的 object 是否可以在 python 中具有不同数量的实例变量。

You're adding an attribute of wheels to the instance firstcar not to the car class .您将wheels属性添加到实例firstcar而不是car class

Each instance has a __dict__ attribute, which holds all other attributes, and effectively all you're doing with每个实例都有一个__dict__属性,该属性包含所有其他属性,并且实际上是您正在使用的所有属性

firstcar.wheels=4 

is

firstcar .__dict__[wheels]=4 
#nb. this won't actually work as you cant assign to object __dict__s this way; 
#it's just an analogy!

This might be clearer now why this doesn't affect secondcar现在可能更清楚为什么这不会影响第二辆车

firstcar = car()
secondcar = car()

firstcar.wheels=4

print(firstcar.__dict__) #{'wheels': 4}
print(secondcar.__dict__)#{}

In contrast you can actually add variables to classes in this way:相反,您实际上可以通过这种方式将变量添加到类中:

class car:
    pass

car.wheels=""

firstcar = car()
secondcar = car()

firstcar.wheels = 4
print(firstcar.wheels)
print(secondcar.wheels)

Which will not give you an attribute error.这不会给你一个属性错误。 In fact you can add attributes to the class after you create your instances and still avoid the error.事实上,您可以在创建实例后将属性添加到 class 并仍然避免错误。

(This is just for explanation, not suggesting to use it in real life, it could lead to some very confusing code!) (这只是为了解释,不建议在现实生活中使用它,它可能会导致一些非常混乱的代码!)

Yes, Two objects of the same class can have different no.是的,同一个 class 的两个对象可以有不同的编号。 of instance variables, But they always have the same no.实例变量,但它们始终具有相同的编号。 of class variables. class 变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何区分同一类的两个不同对象的两个变量? - Python: how can I differentiate between two variables of two different objects of the same class? 为什么相同 class 的两个实例具有不同的属性(Python)是明智的? - Why is it sensible for two instances of the same class to have different attributes (Python)? 可以为不同的类变量调用相同的方法吗 - Can the same method be called for different class variables 为什么同一类的不同对象的方法具有相同的ID? - Why do methods of different objects of same class have same id? Django-只有两个对象的外键不同时,它们的字段才能具有相同的值 - Django - Two objects' field can have the same value only if their foreign keys are different 具有相同列表的两个变量具有不同的ID ...为什么? - Two variables with the same list have different IDs…why is that? 如何将抓取的数据分配给具有相同类的两个不同变量? - How can I assign scraped data two different variables with same class? 使用python定义具有相同列表数据但对象不同的变量 - Define variables with the same list data but different objects using python 我可以使相同值的不同变量指向不同的对象吗? - Can i make different variables of same values point to diffrent objects? Python3将类对象初始化为不同的变量并将其存储在字典中 - Python3 Initializing class objects to different variables and storing them in a dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM