简体   繁体   English

混淆,因为我无法弄清楚是什么改变了 python 中 class 的 object 的属性

[英]confusion because i can't figure out what is changing the attribute of the object of a class in python

Here is my code `这是我的代码`

class Cats:
    def __init__(self,name):
        self.name=name
        #print(self.name,name,"hello")
    def change_name(self,new_name):
        self.name=new_name
        return 0
        #print(new_name)

cat1=Cats("lion")
print(cat1)
print(cat1.name)


cat2=cat1.change_name("tiger")
print(cat1.name)
print(cat1)
print(cat2)

** Here is the output with my comments/opinion on the side (pls correct me if i am wrong): ** <__main__.Cats object at 0x7f84272d7640> error because I tried to print the object cat1 **这是 output ,附上我的评论/意见(如果我错了,请纠正我):** <__main__.Cats object at 0x7f84272d7640>错误,因为我试图打印 object cat1

lion seems fine coz i printed the attribute of the object and since the name given while initialising was lion, it printed lion lion看起来不错,因为我打印了 object 的属性,并且由于初始化时给出的名称是 lion,它打印了 lion

tiger THIS IS WHAT I DON'T UNDERSTAND. tiger这就是我不明白的。 why is this output tiger and not lion.为什么这个 output 是老虎而不是狮子。 what exactly caused this change?究竟是什么导致了这种变化? Bcoz when I do <<cat2=cat1.change_name("tiger"), it should just assign the value 0 to cat2 but why did it change the value in cat1? Bcoz 当我执行 <<cat2=cat1.change_name("tiger") 时,它应该只将值 0 分配给 cat2,但为什么它更改了 cat1 中的值?

<__main__.Cats object at 0x7f84272d7640> error bcoz i tried to print a class <__main__.Cats object at 0x7f84272d7640>错误 bcoz 我试图打印 class

0 seems fine coz chane_name function returns 0 which is assigned to cat2 0似乎很好因为 chane_name function 返回分配给 cat2 的 0

I was expecting the value of cat1.name to remain the same (it should have remained lion and not changed to tiger)我期望 cat1.name 的值保持不变(它应该保持为 lion 而不是更改为 tiger)

When you call cat1.change_name("tiger") , a few things happen:当您调用cat1.change_name("tiger")时,会发生一些事情:

  1. The name self is bound to the same object as cat1 .名称self绑定到与 cat1 相同的cat1
  2. As a result, cat1.name is changed from "lion" to "tiger"结果, cat1.name"lion"更改为"tiger"
  3. change_name returns 0 , not a new instance of Cats . change_name返回0 ,而不是Cats的新实例。

After this call, cat1.name evaluates to "tiger" , not "lion" , because the value has changed.此次调用后, cat1.name计算结果为"tiger" ,而不是"lion" ,因为值已更改。

print(cat1) outputs your "error message" because print implicitly calls str on its argument, and since Cats does not define __str__ , object.__str__ is used instead, and that produces the string you see. print(cat1)输出您的“错误消息”,因为print在其参数上隐式调用str ,并且由于Cats未定义__str__ ,因此使用object.__str__ ,并生成您看到的字符串。

print(cat2) output 0 because that's the value that was assigned to cat2 as the return value of cat1.change_name("tiger") . print(cat2) output 0因为这是作为cat1.change_name("tiger")的返回值分配给cat2的值。

暂无
暂无

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

相关问题 Django / Python API-如何确定方法期望接收的对象类? - Django/Python API - how can I figure out what class of object a method is expecting to receive? Python - 我无法弄清楚的属性错误错误 - “str”对象没有属性“get_message_text” - Python - Attribute Error error I can't figure out - 'str' object has no attribute 'get_message_text' 混淆 Python 中的属性或方法 - Confusion to figure out between attribute or method in Python 我无法弄清楚 python 中的错误 - I can't figure out with an error in python 在python中制作游戏时发生属性错误。 我不知道发生了什么 - Attribute error while making a game in python. I can not figure out what is happening Python 函数正在改变我输入的值,我不知道为什么 - Python function is changing the value of my input, and I can't figure out why Python class 已定义,一切都已定义,但我想不出一种打印方法将其全部打印出来 - Python class is defined, everything is defined but I can't figure out a print method to print it all out 我试图了解类和函数,但似乎无法弄清楚我的代码出了什么问题 - I am trying to understand class and function and can't seem to figure out what is wrong with my code 我似乎无法弄清楚如何将 JSON 对象传递给驻留在不同模块中的类 - I can't seem to figure out how to pass a JSON object to a class residing in a different module 我正在学习python,我无法弄清楚这段代码有什么问题? - I'm learning python, and I can't figure out what's wrong with this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM