简体   繁体   English

什么是属性? 它如何“持有”价值?

[英]What's an attribute? How does it “hold” a value?

What does it mean for an attribute to hold something, and how is it executed in code? 属性包含某些内容意味着什么,以及如何在代码中执行它?

More specifically, what exactly is an attribute in OOP Python? 更具体地说,OOP Python中的属性到底是什么? How can it hold a certain value, for example, "Jack"? 它如何保持某个值,例如“ Jack”? Is an attribute similar to a variable? 属性类似于变量吗? Some questions I'm wondering. 我想知道的一些问题。

An attribute of a class or object is any of the member items: a method, a variable, etc. In this case, it's a variable. 类或对象的属性是任何成员项:方法,变量等。在这种情况下,它是变量。 From this, I assume that you know what it means to hold a value. 由此,我假设您知道持有价值的含义。

Get rid of the string face attribute; 摆脱字符串face属性; this is merely the display rendition of the value. 这仅仅是该值的显示形式。 Store those renditions in a list or dict, and encapsulate that within your display method. 将这些格式副本存储在列表或字典中,并将其封装在显示方法中。 For instance: 例如:

face_image = {
    1: ' ------\n|     |\n| o |\n|     |\n ------',
    2: ' ------\n| o   |\n|   |\n|   o |\n ------',
    ...

... and when you need it ... ...当您需要时...

print(face_image[self.value])

Does that get you moving? 那会让你动起来吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM