0

I am trying to understand where my mistake lies and I was hoping you could please help me.

I have this code:

import copy
class FooInd():
    def __init__(self):
        self.a=1

class Planning():
    def foo(self,pop):
        print(pop.a)

    def main():
        ind=FooInd()
        Planning.foo(copy.deepcopy(ind))
if __name__ == "__main__":
    Planning.main()

However I keep receiving this error:

Planning.foo(copy.deepcopy(ind))
TypeError: foo() missing 1 required positional argument: 'pop'

I believe that the mistake is not in the foo method definition, but in my class initiation of the FooInd, however I have checked the Python documentation for classes and I could not find a solution.

Does anyone have a clue of what could I try or where can I check? Many thanks in advance!

1
  • Planning.foo takes one parameter in addition to the Planning instance self. Commented Dec 2, 2020 at 12:17

2 Answers 2

1

You call Planning.foo on the class, not an instance of the class. You provided the second argument it requires, but not the self argument.

You have two choices:

  1. Construct a Planning instance to call foo on:

    def main():
        ind=FooInd()
        Planning().foo(copy.deepcopy(ind))
        #       ^^ Makes simple instance to call on
    
  2. Make foo a classmethod or staticmethod that doesn't require an instance for self:

    class Planning():
        @staticmethod  # Doesn't need self at all
        def foo(pop):
            print(pop.a)
    
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! That saved me, I was completely clueless! It makes perfect sense!
@dtkx: You're welcome. A heads up: Typically, main is a top-level function, not a method of a class. As written, main isn't even a real method (it doesn't take self, nor is it a @staticmethod, so attempting to call it on an instance of the class would fail); your code only works because it calls it on the class itself. I'd recommend dedenting main out of Planning, and invoking it as plain main() (not Planning.main()) to make it a bit more idiomatic. Sure, a @staticmethod would make it work (whether called on class or instance), but it's silly to make it part of the class.
Thank you so much again! Once again it makes total sense, as you could realize I am new to classes, I am still trying to figure out best practices, organization and trade offs between classes and functions. Your advice certainly helped a lot. Thanks again :)
1

I think you meant to instantiate Planning before calling methods on it:

import copy
class FooInd():
    def __init__(self):
        self.a = 1

class Planning():
    def foo(self, pop):
        print(pop.a)

    def main(self):
        ind = FooInd()
        self.foo(copy.deepcopy(ind))
if __name__ == "__main__":
    p = Planning()
    p.main()

Output:

1

1 Comment

Thaanks! Thats the direction I was missing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.