0

I have created a class called: Sample, where I declared a static method named calculate() which takes a number and return it is square root. This program is saved in 'static_method_2.py'

import math
class Sample:
    data = 10
    @staticmethod
    def calculate(x):
        return(math.sqrt(x))

I want to access this calculate method in another python program.(both python program in same folder)

import static_method_2
num = int(input('Enter Number for square root \n'))
# print(method.data)
print("Square root of {} is {}".format(num, static_method_2.calculate(num)))

When I am running 2nd program, it's showing error:

AttributeError: module 'static_method_2' has no attribute 'calculate'

Can anyone please suggest how to get rid of this problem, or where I am doing mistake.

2 Answers 2

2

Specifying the class name after the module name will do:

print("Square root of {} is {}".format(num, static_method_2.Sample.calculate(num)))
Sign up to request clarification or add additional context in comments.

2 Comments

Why I can't do like this: when I am writing import static_method_2.Sample as sam and trying to use sam.calculate(num), it's not working.
from static_method_2 import Sample as sam and then try to use sam.calculate(num) or import static_method_2.Sample.calculate as sam and then try to use sam.calculate(num)
0

you need to import Sample from the other file and then call Sample. calculate

1 Comment

when I am writing import static_method_2.Sample as sam and trying to use sam.calculate(num), it's not working.

Your Answer

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