Answers for "missing 1 required positional argument, attrs"

5

python missing 1 required positional argument:

def your_method(self, arg1, arg2): # you must add the "self" argument in 1st position
'''
This kind of error happens when working with classes
When you create methods for a class, you must always pass self as the first argument
self designates the object in which it is called
'''
# for instance : 
class Dog: # let create a class Dog
  def __init__(self, color, age): # when you create a new Dog object, two parameters are required
    self.color = color # here, whe use self to attach the attribute to this Dog object
    self.age = age
    self.bark("Woof")
    
  def bark(self, message): # your error might come from the fact you forgot to add the self element as the first argument in, at least, one method
    print(message)
    
'''
This is a very simple example but it stays true for every class
You have to pass self as the first argument for each method
'''
Posted by: Guest on May-12-2021
0

missing 1 required positional argument when calling class function

class Ship:
    def __init__(self, user_x, user_y, user_spaceship_width, user_spaceship_height):
        self.x = user_x
        self.y = user_y
        self.width = user_spaceship_width
        self.height = user_spaceship_height

    def draw_user_ship(self, win):
        win.blit(RED_SPACESHIP, (self.x, self.y, self.width, self.height))

def draw_win():
    win.blit(BG, (0, 0))

    ship = Ship(user_x, user_y, user_spaceship_width, user_spaceship_height)#YOU HAVE TO WRITE THIS BEFORE CALLING THE FUNCTION. THIS IS CALLED INITIATING A FUNCTION.
    ship.draw_user_ship(win)#AFTER YOU'VE INITIATED THE FUNCTION, YOU HAVE TO CALL IT WITH THE NAME YOU'VE INITIATED IT, IN THIS CASE IT'S ship, NOT Ship WITH THE CAPITOL LETTER.

    pygame.display.update()
Posted by: Guest on October-22-2021

Code answers related to "missing 1 required positional argument, attrs"

Python Answers by Framework

Browse Popular Code Answers by Language