sirmaci
sirmaci

Reputation: 93

How can call a class function as same as the main file? (AttributeError)

I have 2 .py extensions called main.py and my_class.py.

main.py:

def foo():
    import my_class
    if my_class.my_inside_func(param) == "True":
        print("congratulations")

in my_class.py I can call the class like this:
my_class.my_inside_func(param)

In main.py I cannot call it like that. The error I get is this:

AttributeError: module my_class has no attribute my_inside_func

I have difficulty understanding what that means.

Thanks in advance.

Upvotes: 0

Views: 54

Answers (3)

Dipen Dadhaniya
Dipen Dadhaniya

Reputation: 4630

Enough information isn't provided. But if the my_class.py is like this:

class my_class:
    @staticmethod
    def my_inside_func():
        pass

Then you should be doing my_class.my_class.my_inside_func(param) instead of my_class.my_inside_func(param) inside main.py.

Upvotes: 0

Stivan
Stivan

Reputation: 1127

def foo():
    from my_class import my_class
    if my_class.my_inside_func(param) == "True":
        print("congratulations")

Upvotes: 0

Darren Christopher
Darren Christopher

Reputation: 4779

import my_class will import the module instead of the class inside it. To import the class inside it use from <module> import <class> since your module and class name is the same, you could do as below

from my_class import my_class

Hope this solves your issue

Upvotes: 1

Related Questions