Kivy/Python: missing 1 required positional argument

5.4k Views Asked by At

I am not too terribly familiar with classes/class methods and how they work, and I know my code is atrocious! I wanted to dip my toe into the world of UI, and so I wanted to take a simple function I wrote in Python to see if I could create an app out of it. (Edit: The script produces a ":(" if today is not Monday, and a ":)" if today IS Monday.) (Double edit: this is my first post, I apologize for my ignorance re: coding and also Stack Overflow formatting.) We've got:

#!/usr/bin/env python                                                                                                          
from kivy.app import App
from kivy.uix.label import Label
import datetime
from datetime import date
import calendar


today = date.today().strftime('%Y-%d-%m')
def findDay(self, today):
    day = datetime.datetime.strptime(today, '%Y-%d-%m').weekday()
    y = (calendar.day_name[day])
    if y == 'Monday':
        x = ':)'
        return x
    else:
        x = ':('
        return x
    print(x)



class MyApp(App):
    def build(self):
        today = date.today().strftime('%Y-%d-%m')
        z = findDay(today)
        return Label(z)

if __name__ == "__main__":
    MyApp().run()

With the error:

Traceback (most recent call last):
   File "main.py", line 30, in <module>
     MyApp().run()
   File "/Users/myusernamehere/opt/anaconda3/lib/python3.7/site-packages/kivy/app.py", line 829, in run
     root = self.build()
   File "main.py", line 26, in build
     z = findDay(today)
 TypeError: findDay() missing 1 required positional argument: 'today'

I know this error arises from incorrect instantiation of an object of a class... but based on how I have defined "today", I am unsure what this means in my given context! (Unless it has to do with "self"?)

3

There are 3 best solutions below

7
On BEST ANSWER

You need self only when you write an instance method (in a class). For normal functions you don't need self in the signature.

This should work fine:

def findDay(today):
    day = datetime.datetime.strptime(today, '%Y-%d-%m').weekday()
    y = (calendar.day_name[day])
    if y == 'Monday':
        x = ':)'
        return x
    else:
        x = ':('
        return x
    print(x)
0
On

I know this error arises from incorrect instantiation of an object of a class... but based on how I have defined "today", I am unsure what this means in my given context! (Unless it has to do with "self"?)

findDay is just a function, it has nothing to do with your class.

As you've defined it it takes two arguments, which you have named self and today. When you call it you pass just one argument (which is bound to the name self, the first argument), and therefore you get the error that you haven't passed 1 required positional argument, the one named today.

Since the function doesn't depend on anything related to your class, I guess you mean it to just be a normal function and you should remove the self parameter as it has no purpose.

0
On

Functions don't have a state, hence they don't need self as a parameter.

#!/usr/bin/env python                                                                                                          
from kivy.app import App
from kivy.uix.label import Label
import datetime
from datetime import date
import calendar

today = date.today().strftime('%Y-%d-%m')
def findDay(today):                            # Changed here
    day = datetime.datetime.strptime(today, '%Y-%d-%m').weekday()
    y = (calendar.day_name[day])
    if y == 'Monday':
        x = ':)'
        return x
    else:
        x = ':('
        return x
    print(x)

class MyApp(App):
    def build(self):
        today = date.today().strftime('%Y-%d-%m')
        z = findDay(today)
        return Label(z)

if __name__ == "__main__":
    MyApp().run()