简体   繁体   English

TypeError:取 0 位置 arguments 但给出 1

[英]TypeError: takes 0 positional arguments but 1 was given

Help me what am I making wrong here since am getting the below error,帮助我,因为出现以下错误,我在这里做错了什么,

TypeError: fizz_buzz() takes 0 positional arguments but 1 was given TypeError: fizz_buzz() takes 0 positional arguments but 1 was given

class FizzBuzz:
    def __init__(self, number_value):
        self.number_value = number_value

    def fizz_buzz():
        if number_value % 3 == 0 and number_value % 5 == 0:
            print("FizzBuzz")
        elif number_value % 3 == 0:
            print("Fizz")
        elif number_value % 5 == 0:
            print("Buzz")
        else:
            return f"{number_value} can't be multiplied by either 3 or 5"

number_value = int(input("Enter number: "))
fizzbuzz_object = FizzBuzz(number_value)
fizzbuzz_object.fizz_buzz()

这意味着你应该在一个类中拥有至少一个参数的所有函数,

def fizz_buzz(self):

Make sure that when you are creating class methods you always have 1 argument called "self":确保在创建类方法时始终有 1 个名为“self”的参数:

def fizz_buzz(self):
    if number_value % 3 == 0 and number_value % 5 == 0:
        print("FizzBuzz")
    elif number_value % 3 == 0:
        print("Fizz")
    elif number_value % 5 == 0:
        print("Buzz")
    else:
        return f"{number_value} can't be multiplied by either 3 or 5"

You need to reference the current (self) instance of the class.您需要引用类的当前(自身)实例。 Try:尝试:

def fizz_buzz(self):

Instead of:代替:

def fizz_buzz():

添加了以下解决问题的代码:

`def fizz_buzz(self):`

Here is the detailed answer:这是详细的答案:

 fizzbuzz_object.fizz_buzz() # you did not pass the instance. "self" is just a symbol that represents the object

fizz_buzz() is a method. fizz_buzz()是一种方法。 Methods are object type in Python.方法是 Python 中的对象类型。 methods are callable like functions but they are bound some object and that object is injected to the method as its first parameter.方法像函数一样可调用,但它们绑定了某个对象,并且该对象作为它的第一个参数注入到方法中。 That is why class methods are called "instance methods".if you run this:这就是为什么类方法被称为“实例方法”。如果你运行这个:

   print(fizzbuzz_object.fizz_buzz)

you will get this:你会得到这个:

    <bound method FizzBuzz.fizz_buzz of <__main__.FizzBuzz object at 0x7ffa306574f0>>

Python sees .fizz_buzz the dot and it knows that this method is bound to the object. Python 看到.fizz_buzz点,它知道这个方法绑定到对象。 this is when fizz_buzz turns to be a method.这就是fizz_buzz变成一种方法的时候。 So far it was considered to be a function.到目前为止,它被认为是一个函数。 This is actually difference between methods and functions.这实际上是方法和函数之间的区别。 methods are bound.方法是绑定的。

type(FizzBuzz.fizz_buzz) is type(fizzbuzz_object.fizz_buzz)

this will return false .这将返回false First type is "function", second type is "method"第一种是“函数”,第二种是“方法”

If you called `FizzBuzz.fizz_buzz` like this you will not get same error.

Behind the scene this is what python calling在幕后这就是python调用的

  FizzBuzz.fizz_buzz(fizzbuzz_object)

With injecting the object into the method, methods can access to the object's namespace.通过将对象注入到方法中,方法可以访问对象的命名空间。 Also this passes 2 attributes to methods这也将 2 个属性传递给方法

print(fizzbuzz_object.fizz_buzz.__self__)
print(fizzbuzz_object.fizz_buzz.__func__)

<__main__.FizzBuzz object at 0x7ffa306574f0>  // object
<function FizzBuzz.fizz_buzz at 0x7ffa306620d0> // method

An instance method needs self in the 1st argument as shown below:实例方法在第一个参数中需要self ,如下所示:

class Person:
            # Here     
    def test(self): 
        print("Test1")

obj = Person()
obj.test1()

Output: Output:

Test1

In detail, I explain about instance method in my answer for What is an "instance method" in Python?详细地说,我在 Python 中对什么是“实例方法”的回答中解释了实例方法 and also explain about @classmethod and @staticmethod in my answer for @classmethod vs @staticmethod in Python :并在 Python 中对@classmethod vs @staticmethod 的回答中解释了@classmethod@staticmethod

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

相关问题 Python:TypeError:draw()接受0个位置参数,但给出了1个 - Python: TypeError: draw() takes 0 positional arguments but 1 was given TypeError:open()接受0个位置参数,但给出了2个 - TypeError: open() takes 0 positional arguments but 2 were given 类型错误:input() 需要 0 个位置参数,但给出了 1 个 - TypeError: input() takes 0 positional arguments but 1 was given TypeError:invoke()接受2个位置参数,但给出了3个 - TypeError: invoke() takes 2 positional arguments but 3 were given TypeError:worker() 接受 0 个位置参数,但给出了 1 个 - TypeError: worker() takes 0 positional arguments but 1 was given TypeError: imwrite() 接受 2 个位置参数,但给出了 3 个 - TypeError: imwrite() takes 2 positional arguments but 3 were given TypeError 接受 2 个位置参数,但给出了 3 个 - TypeError takes 2 positional arguments but 3 were given TypeError: changespeed() 需要 2 个位置 arguments 但给出了 3 个 - TypeError: changespeed() takes 2 positional arguments but 3 were given TypeError: areatriangle() 取 0 个位置 arguments 但给出了 2 个 - TypeError: areatriangle() takes 0 positional arguments but 2 were given /uploaddd/uploaddd() 处的 TypeError 采用 0 位置 arguments 但给出了 1 - TypeError at /uploaddd/ uploaddd() takes 0 positional arguments but 1 was given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM