#๐Ÿ”’ TypeError: function takes 1 positional arguments but 2 were given with OOP?

41 messages ยท Page 1 of 1 (latest)

white merlin
#

I have to use python for this p roject even though I know very little, I taught myself c# and fairly faimilar with it but with python I still have my swimming bands on. In using a state machine to control a virtual pet
I have a parent class called State
I have children classes one of which is Default state

I keep getting this error TypeError: function takes 1 positional arguments but 2 were given

when I call this function

defaultState.py

    def update(self,vpet) -> None:
        super().update(vpet)

which obviously calls the super class

state.py

def update(self,vpet) -> None:
        pass

I call it like so from the vpet class

    def update(self) -> None:
        self.currentState.update(self)

The logic in my brain seems like it should work but it doesnt, I know this is an issue with the coder
the error is thrown at line 7 in defaultState.py where it calls the parent update

abstract hingeBOT
#

@white merlin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

white merlin
#

The reason im passing vpet is because it will hold functions useful to various states and variables

Its a virtual pet so there might be a state for playing with virtual pet so it would need to alter a variable in vpet then another state might need to show the variable

#

tl;dr vpet is thet puppet master that sends info across states and controls the states

slow delta
white merlin
#

which file do you require ?

#

i have the classes in seperate files

slow delta
#

Show everything in one paste

#

!paste

abstract hingeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

slow delta
#

You can add multiple files

white merlin
#

code aint too long i'll post it here

slow delta
#

Best to start with the code

white merlin
#

state.py

class state:
    def __init__(self) -> None:
        pass
    def update(self,vpet) -> None:
        pass
    def show(self,matrix,vpet) -> None:
        pass
    
#

defaultState.py

from state import state
class defaultState(state):
    def __init__(self) -> None:
        super().__init__()
    
    def update(self,vpet) -> None:
        super().update(vpet)

    def show(self, matrix,vpet) -> None:
        matrix.fill((0,0,0))
#

vpet.py

from defaultState import defaultState
class vpet:
    def __init__(self) -> None:
        self.DefaultState = defaultState()
        self.currentState = self.DefaultState
        
    def update(self) -> None:
        self.currentState.update(self)
        
    def show(self,matrix) -> None:
        self.currentState.show(matrix,self)
#

Traceback

Traceback (most recent call last):
  File "<stdin>", line 29, in <module>
  File "vpet.py", line 10, in update
  File "defaultState.py", line 7, in update
TypeError: function takes 1 positional arguments but 2 were given
#

for the record this is micropython on a pi pico

worthy musk
#

hence 2 positional arguments

white merlin
#

but i need the vpet instance to be passed to the currentState so it can interact with the vpet

wet spire
#

it would help a little bit to stick with the standard Python naming scheme

#

don't name anything else than classes with capitalized names

white merlin
#

sorry nothing standards here, ive no professional training

wet spire
#

self.DefaultState -> self.default_state

white merlin
#

alright i can do that

wet spire
#

the distinction makes it clearer what's a class and what's not

white merlin
#

snake case instead of camel case when its not a class

wet spire
#

and always camel case for classes (unless you have a pressing reason to do otherwise)

white merlin
#

alright thanks for that

wet spire
#

so, self.currentState.update(self) <- is there a problem with this now?

white merlin
#

i'll double check now, just renaming things

#

I changed it to self.currentState.update() and it complains of missing the vpet argument

wet spire
#

well obviously because it takes two arguments, yet only self is provided?

white merlin
#

but i want to pass the instance of vpet thats calling current_state.update()

wet spire
#

so pass that argument

white merlin
#

wtf

#

but nothing changedc

#

i call it
self.current_state.update(self)
and it works which is what i did in the first place

abstract hingeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.