Reputation:
I have a problem in my python code. I want to execute functions that are in classes in a file called (functions.py). If I run the main file (main.py), the functions will not work.
Project root:
.________ main.py
project |
|________ functions.py
main.py
import functions
import time
print("Hello World")
time.sleep(1)
functions.clear()
functions.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()
If I run the code, it print this:
I don't know how to fix my error. Please be kind. I'm not advanced in python.
Upvotes: 0
Views: 75
Reputation:
@samarthbhatia put the simpler solution in comments. I rename the functions.py file to wprint.py
In the main.py file, I need to put wprint.functions.clear()
instead of functions.clear
. Thanks samart!
Upvotes: 0
Reputation: 2518
self
in functions
method clear
main.py
import foo
x = foo.functions()
x.clear()
foo.py
class functions:
def clear(self):
print("functions clear")
functions clear
Upvotes: 0
Reputation:
You have forgotten to include the word self
in every function in the class. Also, you need to put it as the first parameter of the function:
def clear(self):
Quoting from geeksforgeeks
self
represents the instance of the class. By using theself
keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes
So all your function definitions inside the class will have a self
parameter as the first one
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()
Upvotes: 1