0

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:

enter image description here

I don't know how to fix my error. Please be kind. I'm not advanced in python.

7
  • 1
    You need to create an instance to functions, not call functions.clear directly. Commented Aug 5, 2021 at 2:14
  • How can I do this? Commented Aug 5, 2021 at 2:15
  • In your own words, where you have class functions: in your code, why have you written this, and what do you think it does? Commented Aug 5, 2021 at 2:17
  • 1
    The problem is you are defining a class functions inside the module functions but only calling the module. Do functions.functions.clear() Commented Aug 5, 2021 at 2:20
  • @KarlKnechtel, i don't understand very well Commented Aug 5, 2021 at 2:20

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Thx, but do I need a __init__() function? And do I need to add self in the function who is in the main file?
@W1L74680. I don't think you need an __init__ function. And yes, you need to create an instance of the class to access the different function. Now, you want to access clear(). So you would do from functions import functions then functions.clear() where functions is the class and clear() is the function
What I was meaning, it's do you write functions in main.py like this: functions.clear(self) or like that functions.clear()
@W1L74680. The second one. The self parameter can help us access the attributes. Not need to pass self as a parameter
0

You have 2 issues in your code:

  1. Don't use python file and class name same, it will confuse you.
  2. As @Sujay said, you forgot a self in functions method clear

sample code:

main.py

import foo
x = foo.functions()
x.clear()

foo.py

class functions:
    def clear(self):
        print("functions clear")

result:

functions clear

Comments

0

@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!

Comments