不使用 def __init__(self) 的 Python 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17331126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:56:38  来源:igfitidea点击:

Python Classes without using def __init__(self)

pythonclass

提问by dman

I am writing a series of text menus. With the class and sub class below it runs with no issues. But I am reviewing my coding and I am wondering....is it ok that I didn't use def __init__(self)in the classes? Should I have placed the data members in def __init__(Self):such as self.images = (), self.options =()? If I did that then I could not use the abc module for restrains, correct?

我正在编写一系列文本菜单。使用下面的类和子类,它运行没有问题。但我正在检查我的编码,我想知道......我没有def __init__(self)在课堂上使用可以吗?我应该将数据成员放在def __init__(Self):诸如 self.images = (), self.options =() 之类的地方吗?如果我这样做了,那么我就不能使用 abc 模块进行限制,对吗?

class BaseMenu(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractproperty
    def options(self):
        pass

    @abc.abstractproperty
    def menu_name(self):
        pass

    def display(self):
        header = "FooBar YO"
        term = getTerminalSize()
        #sys.stdout.write("\x1b[2J\x1b[H")
        print header.center(term, '*')
        print self.menu_name.center(term, '+')
        print "Please choose which option:"
        for i in self.options:
            print(
                str(self.options.index(i)+1) + ") "
                + i.__name__
            )
        value = int(raw_input("Please Choose: ")) - 1

        self.options[value](self)

class Servers(BaseMenu):

    menu_name = "Servers"
    images = ()
    foo = ()

    def get_images(self):
        if not self.images:
            self.images = list_images.get_images()
        for img in self.images:
            print (
                str(self.images.index(img)+1) + ") "
                + "Name: %s\n    ID: %s" %
                (img.name, img.id)
                )

    def get_foo(self):
        if not self.foo:
            self.foo = list_list.get_list()
        for list in self.foo:
            print "Name:", list.name
            print "  ID:", list.id
            print

    def create_servers(self):
         create_server.create(self)

    options = (
        get_images,
        get_foo,
        create_servers
        )

采纳答案by Martijn Pieters

Your code is perfectly fine. You don't haveto have an __init__method.

你的代码非常好。你不具备有一个__init__方法。

You can still use __init__, even with an ABC. All that the ABC meta tests for is if the nameshave been defined. Setting imagesin an __init__does requires that you define a class attribute, but you can set that to Noneat first:

__init__即使使用 ABC,您仍然可以使用。ABC 元测试的所有内容是名称是否已定义。images在do中设置__init__需要您定义一个类属性,但您可以首先将其设置为None

class Servers(BaseMenu):

    menu_name = "Servers"
    images = None
    foo = None

    def __init__(self):
        self.images = list_images.get_images()
        self.foo = list_list.get_list()

Now you can set constraints on the ABC requiring that a imagesabstract property be available; the images = Noneclass attribute will satisfy that constraint.

现在您可以在 ABC 上设置约束,要求images抽象属性可用;在images = None类属性将满足该约束。

回答by Chandan

class A:
    def a(self, a):
        print(a)
ob = A()
ob.a("Hello World")