I'm working on a logging libary. I want to transform my class, so that it's possible to have instances of it. My idea how it should look like is following:
from froggius import Froggius
instance = Froggius()
instance.debug('Hello World')
My class is simply built like every other; init function with some vars and other functions like debug or warning with some parameters. At this moment, when I run code like above, I'm getting this output:
[DBG] [03/04/2024 15:16:15] <froggius.logger.Froggius object at 0x1001eda00>
The class starts like that:
class Froggius():
"""
Main class of Froggius
Includes logging methods
"""
def __init__(self, file_path=None, print_out=None) -> None:
global glob_file_path, glob_print_out
if file_path is not None:
glob_file_path = file_path
else:
glob_file_path = None
if print_out is not None:
glob_print_out = print_out
else:
glob_print_out = None
def debug(log_msg, file_path=None, print_out=None):
"""
Writes logs, optionally to a file.
Parameters
----------
log_msg : str
The message to be logged.
file_path : str, optional
The path to the file where the log should be saved, by default None
highliting : bool, optional
Whether the DEBUG tag should be highlighted with ANSI escape sequences, by default True
print_out : bool, optional
Whether the log should be printed to the stdout, by default True
"""
As others have pointed out in the comments, classes in Python can already have instances by default.
Following this you can create as many instances as you would like, here's an example of how to do it from W3 schools:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
As you can see it is pretty much the same thing you wrote, so just assign a variable to the object in order to create the instance like so:
instance1 = Froggius()
instance2 = Froggius()
and so on. For more information on Python classes I recommend checking out this page from the W3 schools website.