3

I have written a class like below:

class Logger:

    @staticmethod
    def get_timestamp():
        import datetime
        return datetime.datetime.utcnow()
    
    def print_log(self,color, write_level, msg):
        return color

    def log_level_print(self,log_level, write_level, msg):
        if log_level == 'ERROR':
            return print_log(bcolors.FAIL, write_level, msg)
        if log_level == 'WARN':
            return print_log(bcolors.WARNING, write_level, msg)
        if log_level == 'INFO':
            return print_log(bcolors.OKGREEN, write_level, msg)
        if log_level == 'DEBUG':
            return print_log(bcolors.OKBLUE, write_level, msg)
        else:
            print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
            return

Here, I am using this class :

from logger import Logger
demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))

I am not able to call this function, I'm getting an error:
NameError: name 'print_log' is not defined

maciejwww
  • 1,067
  • 1
  • 13
  • 26
Jithin Zacharia
  • 341
  • 1
  • 3
  • 17
  • Shouldn't you give the function 3rd argument? – maciejwww Sep 17 '20 at 11:45
  • You forgot the `self` as first argument in the definition of `log_level_print`... (same goes for `print_log` by the way) – Tomerikoo Sep 17 '20 at 11:49
  • The first argument should be `self`, change it to `log_level_print(self, log_level, write_level, msg`). – bertdida Sep 17 '20 at 11:49
  • Does this answer your question? [TypeError: method() takes 1 positional argument but 2 were given](https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given) – Tomerikoo Sep 17 '20 at 11:51

3 Answers3

5

You forgot to add self as the first argument of class' methods and when you use the methods. Corrected code:

class Logger:
    @staticmethod
    def get_timestamp():
        import datetime
        return datetime.datetime.utcnow()

    def print_log(self, color, write_level, msg):
        return color

    def log_level_print(self, log_level, write_level, msg):
        if log_level == 'ERROR':
            return self.print_log(bcolors.FAIL, write_level, msg)
        if log_level == 'WARN':
            return self.print_log(bcolors.WARNING, write_level, msg)
        if log_level == 'INFO':
            return self.print_log(bcolors.OKGREEN, write_level, msg)
        if log_level == 'DEBUG':
            return self.print_log(bcolors.OKBLUE, write_level, msg)
        else:
            print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
            return

Look, it's a code I'm running:

demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))

and this is a result:
2

wjandrea
  • 28,235
  • 9
  • 60
  • 81
maciejwww
  • 1,067
  • 1
  • 13
  • 26
2

I think you're missing a self. All (EDIT: non static, nor abstract) class methods should have a parameter which is typically named self, like so:

class Logger():
    @staticmethod
    def get_timestamp():
        ...

    def print_log(self, color, write_level, msg):
        ...

    def log_level_print(self, log_level, write_level, msg):
        ...


demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
Rocco Fortuna
  • 300
  • 1
  • 11
1

As mentioned you need to call self and scondly it will still throw error if bcolors is not specified. You need to call bcolors. I am assuming that it is some another dependency which you have not specified.

Here is a sample.

class Logger:
    @staticmethod
    def get_timestamp(self):
        import datetime
        return datetime.datetime.utcnow()

    def print_log(self, color, write_level, msg):
        return color

    def log_level_print(self, log_level, write_level, msg):

        bcolors = []  # temporary empty list

        if log_level == 'ERROR':
            return self.print_log(bcolors.FAIL, write_level, msg)
        if log_level == 'WARN':
            return self.print_log(bcolors.WARNING, write_level, msg)
        if log_level == 'INFO':
            return self.print_log(bcolors.OKGREEN, write_level, msg)
        if log_level == 'DEBUG':
            return self.print_log(bcolors.OKBLUE, write_level, msg)
        else:
            print(f"{Logger.get_timestamp()} {bcolors.FAIL}: Invalid LOG type{bcolors.ENDC}")
            return

demo = Logger()
print(demo.log_level_print('ERROR','ssdsd','sdsdsd'))
Dharman
  • 30,962
  • 25
  • 85
  • 135
der_radler
  • 549
  • 1
  • 6
  • 17