TypeError: method() takes 1 positional argument but 2 were given

前端 未结 9 969
孤街浪徒
孤街浪徒 2020-11-22 05:02

If I have a class...

class MyClass:

    def method(arg):
        print(arg)

...which I use to create an object...

my_objec         


        
相关标签:
9条回答
  • 2020-11-22 05:46

    As mentioned in other answers - when you use an instance method you need to pass self as the first argument - this is the source of the error.

    With addition to that,it is important to understand that only instance methods take self as the first argument in order to refer to the instance.

    In case the method is Static you don't pass self, but a cls argument instead (or class_).

    Please see an example below.

    class City:
    
       country = "USA" # This is a class level attribute which will be shared across all instances  (and not created PER instance)
    
       def __init__(self, name, location, population):
           self.name       = name
           self.location   = location
           self.population = population
     
       # This is an instance method which takes self as the first argument to refer to the instance 
       def print_population(self, some_nice_sentence_prefix):
           print(some_nice_sentence_prefix +" In " +self.name + " lives " +self.population + " people!")
    
       # This is a static (class) method which is marked with the @classmethod attribute
       # All class methods must take a class argument as first param. The convention is to name is "cls" but class_ is also ok
       @classmethod
       def change_country(cls, new_country):
           cls.country = new_country
    

    Some tests just to make things more clear:

    # Populate objects
    city1 = City("New York",    "East", "18,804,000")
    city2 = City("Los Angeles", "West", "10,118,800")
    
    #1) Use the instance method: No need to pass "self" - it is passed as the city1 instance
    city1.print_population("Did You Know?") # Prints: Did You Know? In New York lives 18,804,000 people!
    
    #2.A) Use the static method in the object
    city2.change_country("Canada")
    
    #2.B) Will be reflected in all objects
    print("city1.country=",city1.country) # Prints Canada
    print("city2.country=",city2.country) # Prints Canada
    
    0 讨论(0)
  • 2020-11-22 05:49

    In simple words.

    In Python you should add self argument as the first argument to all defined methods in classes:

    class MyClass:
      def method(self, arg):
        print(arg)
    

    Then you can use your method according to your intuition:

    >>> my_object = MyClass()
    >>> my_object.method("foo")
    foo
    

    This should solve your problem :)

    For a better understanding, you can also read the answers to this question: What is the purpose of self?

    0 讨论(0)
  • 2020-11-22 05:51

    You should actually create a class:

    class accum:
        def __init__(self):
            self.acc = 0
        def accumulator(self, var2add, end):
            if not end:
                self.acc+=var2add
        return self.acc
    
    0 讨论(0)
提交回复
热议问题