Как использовать методы класса из импортируемого своего модуля?

RuslanBay

Новичок
Пользователь
Фев 25, 2022
8
1
1
Я создал свой класс в отдельном файле MyMath.py

Вот его код

Python:
import math
from abc import ABC


class MyMath(ABC):
    """Абстрактный класс MyMath"""


    @classmethod
    def circle_len(cls, radius: int) -> float:
        return 2 * math.pi * radius


    @classmethod
    def circle_sq(cls, radius: int) -> float:
        return math.pi * radius ** 2


    @classmethod
    def cube_value(cls, edge: int) -> float:
        return edge ** 3


    @classmethod
    def sphere_area(cls, radius: int) -> float:
        return 4 * math.pi * radius ** 2

С помощью import MyMath импортирую его в файл main.py . Они лежат в одной директории.

Как мне использовать методы класса MyMath?

На данный момент при написании в main.py:

Python:
import MyMath


res_1 = MyMath.circle_len(radius=5)
print(res_1)


res_2 = MyMath.circle_sq(radius=6)
print(res_2)


res_3 = MyMath.cube_value(edge=6)
print(res_3)


res_4 = MyMath.sphere_area(radius=6)
print(res_4)

выходит ошибка AttributeError: module 'MyMath' has no attribute 'circle_len'

не видит аттрибутов
 

RuslanBay

Новичок
Пользователь
Фев 25, 2022
8
1
1
Я создал свой класс в отдельном файле MyMath.py

Вот его код

Python:
import math
from abc import ABC


class MyMath(ABC):
    """Абстрактный класс MyMath"""


    @classmethod
    def circle_len(cls, radius: int) -> float:
        return 2 * math.pi * radius


    @classmethod
    def circle_sq(cls, radius: int) -> float:
        return math.pi * radius ** 2


    @classmethod
    def cube_value(cls, edge: int) -> float:
        return edge ** 3


    @classmethod
    def sphere_area(cls, radius: int) -> float:
        return 4 * math.pi * radius ** 2

С помощью import MyMath импортирую его в файл main.py . Они лежат в одной директории.

Как мне использовать методы класса MyMath?

На данный момент при написании в main.py:

Python:
import MyMath


res_1 = MyMath.circle_len(radius=5)
print(res_1)


res_2 = MyMath.circle_sq(radius=6)
print(res_2)


res_3 = MyMath.cube_value(edge=6)
print(res_3)


res_4 = MyMath.sphere_area(radius=6)
print(res_4)

выходит ошибка AttributeError: module 'MyMath' has no attribute 'circle_len'

не видит аттрибутов
from MyMath import MyMath
 
  • Мне нравится
Реакции: Student

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 677
479
83
или res_1 = MyMath.MyMath.circle_len(radius=5)
 

Форум IT Специалистов