0

My a.py file is like this

class sumofnumbers:
     def __init__(self):
         .....
     def sum(self,number):
        ....

The Run.py file calls this file as:

import a
l=[1,2]
sum=a.sumofnumbers.sum(l)

This is throwing an error as:

sum() missing 1 required positional argument: 'number'

Both files are in same directory. Please tell me what I am doing wrong

Shailly
  • 23
  • 4
  • 3
    You never created a sumofnumbers instance. – The Photon Jun 02 '22 at 23:14
  • 3
    `a.sumofnumbers` should be `a.sumofnumbers()`. You have to call the class to create an instance. – Barmar Jun 02 '22 at 23:16
  • 3
    This has nothing to do with the function being in another python file. This is how you always create class instances. – Barmar Jun 02 '22 at 23:17
  • 2
    https://docs.python.org/3/tutorial/classes.html – njp Jun 02 '22 at 23:19
  • "How to pass arguments to a function in another python file" The same way that you do to a function in the same file. However, this is a method, not a function. "which has argument "self" in it?" Because it's a method of a class, you need an instance of the class first - the same way that you would if the class were written in the same file. – Karl Knechtel Jun 03 '22 at 01:51

0 Answers0