Why do I get "missing 1 required positional argument: 'self'"?
On both classes create_querauto is an instance method, meaning that the class needs to be instantiated in order to call the method.
That's why it is working with the player instance.
i.e.
class Player:
def create_querauto(self):
ii=random.randrange(0,8)
img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png")
img = pygame.transform.scale(img,(100,25))
return QuerWagen(200,300,img)
player_instance = Player()
player_instance.create_querauto() # This will work
Player.create_querauto() # This will throw the error you see above
If you would like to method to work without instantiating the class first you can define it as a static method, using the @staticmethod decorator
This would look like:
class QuerWagen(pygame.sprite.Sprite):
@staticmethod
def create_querauto():
ii=random.randrange(0,8)
img = pygame.image.load(f"Bilder/Gegenstaende/geg{ii}.png")
img = pygame.transform.scale(img,(100,25))
return QuerWagen(200,300,img)
Now you can call that function on the class directly like you were trying to do.
querwagen = QuerWagen.create_querauto() # works fine
Python class showing "missing 1 required positional argument: 'self'"
You should call the method on the instance not the class object.
john_smith.f_salary()
john_smith.display()
Because the self
parameter is a reference to the class instance which called the method in the first place.
Type Error missing 1 required positional argument: 'self' calling the class function
Static method use only the namespace of the class. It can use class variables but not the ones specific to an object (self
).
If all of them are static methods, then you should change the code as follows because there is no notion of self
in static methods.
class Interface:
users_list = []
admins_list = []
categories_list = []
current_user = ''
user_flag = 0
@staticmethod
def start_menu():
print('Add user')
Interface.create_user()
Interface.show_menu()
@staticmethod
def create_user():
# code....
@staticmethod
def choose_user():
# code...
# call
Interface.start_menu()
If you need self
, make those methods instance methods by removing the staticmethod
decorator. Also, since you have no arguments for the constructor you can make those variables static, like above.
class Interface:
def __init__(self):
self.users_list = []
self.admins_list = []
self.categories_list = []
self.current_user = ''
self.user_flag = 0
def start_menu(self):
print('Add user')
self.create_user()
self.show_menu()
def create_user(self):
# code....
def choose_user(self):
# code...
# call
inter = Interface()
inter.start_menu()
Due to the above-mentioned constraints, an instance method (method referring to the object, i.e., self
) can call static methods but not vice-versa.
TypeError at / save() missing 1 required positional argument: 'self'
Views.py
def cash_in(request, amount):
# user whose total_amount is to be updated.
user = request.user
# get current user instance of balance_data
user_current_balance = balance_data.objects.get(user=user)
# update total_amount by adding amount to its total_amount
user_current_balance.total_amount = user_current_balance.total_amount + amount
# save changes to database
user_current_balance.save()
return HttpResponse(user_current_balance.total_amount)
Related Topics
How to Locate Elements on Webpage With Headless Chrome
Python Pip Install Fails: Invalid Command Egg_Info
How to Convert .Dat to .Csv Using Python
Discord.Py Show Who Invited a User
How to Replace Nan Values Where the Other Columns Meet a Certain Criteria
Xlsxwriter: How to Open an Existing Worksheet in My Workbook
How to Assign and Use Column Headers in Spark
Moving Position of Character Within an Item in List
How to Stagger or Offset X-Axis Labels in Matplotlib
How to Send Smtp Email for Office365 With Python Using Tls/Ssl
Pandas Dataframe Check If Column Value Exists in a Group of Columns
How to Split a CSV File Row to Columns in Python
How to Pad a String With Leading Zeros in Python 3
Convert String to Python Class Object
Parsing Outlook .Msg Files With Python
Creating a New Dataframe Column by Comparing Strings of Two Unequal Dataframes
Get All the Diagonals in a Matrix/List of Lists in Python
How to Find the Closest Values in a Pandas Series to an Input Number