简体   繁体   English

Python类保持转义序列

[英]Python class to keep escape sequences

I have created simple class to keep escape seqs: 我创建了简单的类以保持转义seqs:

class Color:
    HEAD = '\033[95m'
    OKBL = '\033[94m'
    OKGR = '\033[92m'
    FAIL = '\033[93m'
    WARN = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDR = '\033[4m'

to use them in console outputs like so: 在控制台输出中使用它们,如下所示:

print(Color.WARN + "Output file will be overwritten!" + Color.ENDC)

now get message from linter that class has no __init__ method. 现在从linter获取该类没有__init__方法的消息。

Is it OK and I can simply ignore this warning or what would be pythonic way here? 它可以,我可以简单地忽略这个警告或者这里的pythonic方式是什么?

This would be the pythonic way to assign variables inside the class and access them outside. 这将是在类中分配变量并在外部访问它们的pythonic方法。

class Color:
  def __init__(self):
    self.HEAD = '\033[95m'
    self.OKBL = '\033[94m'
    self.OKGR = '\033[92m'
    self.FAIL = '\033[93m'
    self.WARN = '\033[91m'
    self.ENDC = '\033[0m'
    self.BOLD = '\033[1m'
    self.UNDR = '\033[4m'

color = Color()
print(color.WARN + "Output file will be overwritten!" + color.ENDC)

__init__() method is used while creating an object from class. 从类创建对象时使用__init__()方法。 If you do not want to create any objects you do not have to define it. 如果您不想创建任何对象,则无需定义它。 However, if you only want to it use to separate namespaces you could also simply move it to separate module. 但是,如果您只想使用它来分隔命名空间,您也可以简单地将其移动到单独的模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM