访问类中其他地方的函数和变量(Python)

2024-04-19 14:53:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循的教程来自:http://sthurlow.com/python/lesson08/

我不理解以下内容:

Any function or variable created on the first level of indentation (that is, lines of code that start one TAB to the right of where we put class Shape is automatically put into self. To access these functions and variables elsewhere inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name).

下面是所用示例的一部分:

#An example of a class
class Shape:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    description = "This shape has not been described yet"
    author = "Nobody has claimed to make this shape yet"
    def area(self):
        return self.x * self.y
    def perimeter(self):
        return 2 * self.x + 2 * self.y
    def describe(self,text):
        self.description = text

我理解self.x是如何影响代码的uuuuinit uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。。。换句话说,我试图弄清楚self.x在函数中做了什么。如果我只在函数中加x,它做什么?如果我把x放在int中,它只“存在”int中,当我创建一个对象时不能被调用。如果我把self.x放在int中,当我创建一个对象时就可以调用它。我想知道self.x和函数中的x,因为我无法找出测试它的代码


Tags: andoftheto函数nameselfthat
1条回答
网友
1楼 · 发布于 2024-04-19 14:53:41

I understand how self.x affects __ init __ part of the code, but not for functions because they seem to play by different rules (e.g. i cannot access variables from inside functions)...

不,他们真的不遵守不同的规则__init__只是一个函数,在class定义中定义,与area完全相同

它们都将self作为显式参数,如果要访问或设置x之类的实例属性,或调用describe之类的实例方法,则必须使用该self

唯一的区别是它们的名称:

  • area是您直接调用的东西。编写my_shape.area()时,调用area函数,将my_shape作为self的值传递
  • __init__是Python自动调用的东西。编写my_shape = Shape(2, 3)时,Python构造一个新的Shape对象,然后调用__init__函数,将该新对象作为self(而23作为xy)传递

In other words, i'm trying to figure out what self.x is doing in the functions. if I put only x in the function what does it do?

普通旧的x是局部变量(如果有),是全局变量(如果没有)。所以,在__init__内部,有一个名为x的参数,它是x(例如,在Shape(2, 3)示例中是2)。在area内部,没有任何名为x的局部变量,它将是一个全局变量。但是您可能也没有一个名为x的全局变量,因此它会引发NameError

另一方面,self.x是任何selfx属性。如上所述,self是在__init__内新创建的Shape实例,以及在area内调用的Shape实例


If I put x in __ int __ it only 'lives' in __ int and cannot be called when I make an object .

是的,如果在__init__中定义名为x的东西,它是一个局部变量,因此它只存在于__init__中。这对于任何函数都是如此,不仅仅是__init__,甚至不仅仅是类中定义的方法;这就是局部变量的含义。一旦函数结束,这些变量就消失了,没有人能再访问它们(如果涉及到闭包,这是不太正确的,但是它们不在这里,所以忽略它。)

我不知道你所说的“被调用”是什么意思,因为你通常不会调用那些不是函数/方法/类的值,我也不知道你所说的“当我生成一个对象时”是什么意思,因为当你生成一个对象时,正好是__init__被调用的时候

If I put self.x in __int it can be called when I make an object.

__init__内分配给self.x的任何内容都将作为self实例的一部分存储。因此,拥有该实例的任何人都可以再次访问它。例如,在area内部,您可以作为self.x访问它。或者,从顶层代码中,您可以作为my_shape.x访问它

再说一次,这里的__init__没有什么特别之处;您可以在另一个方法中执行与describe方法相同的操作。你甚至可以在物体外面做

例如:

>>> my_shape = Shape(2, 3)
>>> my_shape.x
2
>>> my_shape.area()
6
>>> my_shape.x = 4
>>> my_shape.area()
12

再说一次,我不知道你所说的“召唤”或“当我制造一个物体时”是什么意思

I am wondering about self.x vs x in functions because I cannot figure out code to test it

尝试添加此方法:

def play_with_x(self):
    x = 10
    print(x)
    print(self.x)
    x = 20
    print(x)
    print(self.x)
    self.x = 30
    print(x)
    print(self.x)

那么试试这个:

>>> x = 0
>>> my_shape = Shape(2, 3)
>>> my_shape.play_with_x()

您将看到它可以改变xself.x。它们彼此完全独立,但在一个函数中,它们的行为似乎基本相同。但现在:

>>> x
0
>>> my_shape.x
30

x = 20对全局变量x没有任何作用。但是self.x = 30确实永久地改变了self,它和my_shape是同一个对象,所以my_shape.x现在是30

相关问题 更多 >