首页 > 解决方案 > 其他文件中的类中的函数(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方面并不先进。

标签: pythonpython-3.xclass

解决方案


您忘记self在类中的每个函数中包含该词。此外,您需要将其作为函数的第一个参数:

def clear(self):

引用geeksforgeeks

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()

推荐阅读