python - 其他文件中的类中的函数(python)
问题描述
我的 python 代码有问题。我想执行一个名为(functions.py)的文件中的类中的函数。如果我运行主文件(main.py),这些功能将不起作用。
项目根:
.________ main.py
project |
|________ functions.py
主文件
import functions
import time
print("Hello World")
time.sleep(1)
functions.clear()
函数.py
import time
import os
import sys
import math
class color:
PURPLE = "\033[95ml"
BLACK = "\033[30m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINED = "\033[4m"
MAGENTA = "\033[35m"
GREY = "\033[90m"
ITALIC = "\033[3m"
END = "\033[0m"
class functions:
def clear():
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
def animation(text):
for letter in text:
time.sleep(0.01)
sys.stdout.write(letter)
sys.stdout.flush()
class mathematics:
def add(a, b):
print(a + b)
def subtract(a, b):
print(a - b)
def multiply(a, b):
print(a * b)
def divide(a, b):
print(a / b)
class colors:
def red_text(text):
print(f"{color.RED}{text}{color.END}")
def blue_text(text):
print(f"{color.BLUE}{text}{color.END}")
def yellow_text(text):
print(f"{color.YELLOW}{text}{color.END}")
def purple_text(text):
print(f"{color.PURPLE}{text}{color.END}")
def cyan_text(text):
print(f"{color.CYAN}{text}{color.END}")
def darkcyan_text(text):
print(f"{color.DARKCYAN}{text}{color.END}")
def green_text(text):
print(f"{color.GREEN}{text}{color.END}")
def black_text(text):
print(f"{color.BLACK}{text}{color.END}")
def magenta_text(text):
print(f"{color.MAGENTA}{text}{color.END}")
class markdown:
def bold_text(text):
print(f"{color.BOLD}{text}{color.END}")
def underlined_text(text):
print(f"{color.UNDERLINED}{text}{color.END}")
def italic_text(text):
print(f"{color.ITALIC}{text}{color.END}")
def highlight_text(text):
print(f"{color.HIGHLIGHT}{text}{color.END}")
def ready():
functions.animation(f"{color.RED}You are using the Print-functions library by W1L7{color.END}")
time.sleep(1)
functions.clear()
ready()
如果我运行代码,它会打印:
我不知道如何解决我的错误。请善待。我在python方面并不先进。
解决方案
您忘记self
在类中的每个函数中包含该词。此外,您需要将其作为函数的第一个参数:
def clear(self):
self
表示类的实例。通过使用self
关键字,我们可以访问python中类的属性和方法。它将属性与给定的参数绑定。你需要使用 self. 是因为Python不使用@语法来引用实例属性
所以你在类中的所有函数定义都会有一个self
参数作为第一个参数
class functions:
def clear(self):
....
def animation(self,text):
...
class mathematics:
def add(self,a, b):
...
def subtract(self,a, b):
...
def multiply(self,a, b):
...
def divide(self,a, b):
...
class colors:
def red_text(self,text):
...
def blue_text(self,text):
...
def yellow_text(text):
...
def purple_text(self,text):
...
def cyan_text(self,text):
...
def darkcyan_text(self,text):
...
def green_text(self,text):
...
def black_text(self,text):
...
def magenta_text(self,text):
...
class markdown:
def bold_text(self,text):
...
def underlined_text(self,text):
....
def italic_text(self,text):
...
def highlight_text(self,text):
...
def ready():
functions.animation(f"{color.RED}You are using the Print-functions library by W1L7{color.END}")
time.sleep(1)
functions.clear()
ready()
推荐阅读
- elasticsearch - Percentage for each bucket
- python - 在抓取 Web 内容时,无法使用先例函数输出作为下一个函数的输入
- react-native - 如何在博览会中删除发布代码更新?
- android - 对新项目使用本地 gradle 分发而不是包装器
- c# - 如何在 c# uwp 中创建一个自动执行某些操作的输入框?
- gitlab - 如何在 gitlab-ce 中启用功能标志?
- javascript - 如何编写代码以附加代码并从给定的 json 数据运行值
- php - Laravel 迁移抛出外键异常
- python - 如何使用 Pandas 读取 CSV 并仅将其读入 1 列而没有 Sep 或 Delimiter
- c++ - 具有多个属性的预定义运算符