简体   繁体   English

为什么这个类的方法的实例不使用此参数?

[英]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. selfsquare一个实例,例如square(3)返回的东西。

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

相关问题 class 实例可以带参数吗? - Can a class instance take an argument? 在 class 的实例中将字符串作为参数传递,应该具有将字符串作为 arg 并根据 instance.method 执行的方法 - pass a string as argument in instance of a class, should have method that take string as an arg and perform according to instance.method 为什么“到期”参数不考虑时间戳的时间部分? - Why the "due" argument doesn't take the in account the time portion of the timestamp? 为什么在 class 方法中创建 class 的实例会更改“self”参数? - Why creating an instance of a class within a class method changes the 'self' argument? 为什么 function 不改变它的论点? - Why doesn't the function change it's argument? 在方法参数中传递类实例 - Passing class instance in the method argument 为什么Python在实例创建时调用实例方法__init __()而不是调用类提供的__init __()? - Why doesn't Python call instance method __init__() on instance creation but calls class-provided __init__() instead? 为什么 class 不从列表中获取信息? Python - Why doesn't the class take the info from list? Python 调用类实例方法不打印任何内容 - Callling class instance method doesn't print anything 为什么类的 static 变量不能是 class 的实例? - Why can't a class's static variable be an instance of the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM