[英]Why doesn't this instance of this class's method take this argument?
My question is why I can't make a object of the class giving it an integer as an argument. 我的问题是为什么我不能使该类的对象给它一个整数作为参数。 It totally ignores the value that I pass as an argument to it. 它完全忽略了我作为参数传递的值。 Yet, if I call the area function from the class and pass the same number into it's parentheses it will output the desired result of 144. 但是,如果我从类中调用area函数并将相同的数字传递到它的括号中,它将输出144的期望结果。
My best guess from what I have read so far is that the way the classes function is coded is incorrect for what I am hoping that it will achieve. 到目前为止,我最大的猜测是,类函数的编码方式对于我希望实现的目标而言是不正确的。 Do I need to have the area function take it's argument like: self.length
? 我是否需要area函数接受类似self.length
的参数?
# -*- coding: utf-8 -*-
class square:
sides = 4
def __init__(self, length):
self.length = length
def area(self, length):
return length * length
box = square(12)
print(box.area())
Output: TypeError: area() missing 1 required positional argument: 'length'
输出: TypeError: area() missing 1 required positional argument: 'length'
Shouldn't the object that is created save the value that is given to it for the duration of that objects life? 创建的对象是否不应该在该对象的生命期内保存为其赋予的值? Why does it throw another error when I tell it to print from the area method a second time if the calls looked like this? 如果调用看起来像这样第二次从area方法打印时,为什么还会引发另一个错误?
print(box.area(12))
output:144
print(box.area())
output: <bound method square_shape.area of <__main__.square_shape object at 0x7f5f88355b70>>
I'm sorry if this question is a little oddly phrased but I'm simply looking for as much information as I can get and attempting to gain a better understanding of what I'm doing here. 很抱歉,这个问题的措词有些奇怪,但我只是在寻找尽可能多的信息,并试图更好地了解我在这里所做的事情。 Other questions on this subject didn't give a good grasp of what I'm trying to figure out. 关于这个问题的其他问题并不能很好地理解我要解决的问题。
You are thinking of self.length. 您正在考虑self.length。 length (without a "self." in front of it) is just a local variable. 长度(前面没有“自我”)只是局部变量。 If it's an argument, you need to provide it. 如果是参数,则需要提供。
What you probably want is: def area(self): return self.length * self.length
您可能想要的是: def area(self): return self.length * self.length
Your area
takes the self
argument, which is enough to access self.length
: 您所在的area
采用self
参数,足以访问self.length
:
...
def area(self):
return self.length * self.length
Then you can call square(3).area()
and get 9
. 然后可以调用square(3).area()
并得到9
。
Your current declaration requires an explicit length argument, something like square(3).area(4)
which would produce 16
. 您当前的声明需要一个显式的长度参数,类似于square(3).area(4)
,它将产生16
。
One of the key tenets of OOP is that methods have access to object's (or often said instance's ) state, and can operate on that state, instead of passing it explicitly. OOP的主要原则之一是方法可以访问对象(或通常所说的实例 )的状态,并且可以在该状态下操作,而不必显式传递它。
In Python, the parameter normally named self
is used to pass the object in question to the method. 在Python中,通常称为self
的参数用于将相关对象传递给方法。 The area
method operates on square
's length
; area
法在square
length
上运算; you access it as self.length
. 您以self.length
访问它。 That self
is an instance of square
, the thing that eg square(3)
returns. 该self
是square
一个实例,例如square(3)
返回的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.