Python类继承函数并从子类传递参数

Python类继承函数并从子类传递参数,python,class,inheritance,Python,Class,Inheritance,尝试从主菜单->服务器子菜单开始创建一系列文本菜单。当Servers()从BaseClass继承display()时,如何使继承的函数display()接收Servers()类中包含的选项元组和sub_menu=“Server menu”字符串?您可以在display函数中使用self.options和self.sub_menu,但是为什么您在基本菜单类中引用它们,而该类对选项或子菜单一无所知 第二个问题是,您正在将“server”参数传递给一个类,该类的\uuu init\uuu不接受任何参数

尝试从主菜单->服务器子菜单开始创建一系列文本菜单。当Servers()从BaseClass继承display()时,如何使继承的函数display()接收Servers()类中包含的选项元组和sub_menu=“Server menu”字符串?

您可以在
display
函数中使用
self.options
self.sub_menu
,但是为什么您在
基本菜单
类中引用它们,而该类对
选项
子菜单
一无所知

第二个问题是,您正在将
“server”
参数传递给一个类,该类的
\uuu init\uuu
不接受任何参数,因此您需要添加该参数

如果您打算从不实例化
BaseMenu
对象,那么它就是一个抽象基类(或ABC)。您可以使用pythons
abc
module来定义它,以确保继承类定义了您期望的属性:

class BaseMenu(object):
    def display(self):
        header = "FooBar YO"
        term = getTerminalSize()
        #sys.stdout.write("\x1b[2J\x1b[H")
        print header.center(term, '*')
        #print sub_menu.center(term, '+')
        print "Please choose which option:"
        for i in options:
            print(
                str(options.index(i)+1) + ") "
            )

class Servers(BaseMenu):
    def __init__(self):
        #super(Servers, self).__init__("server")
        pass

    def list_foo(self):
        pass
    def list_bar(self):
        pass
    options = (
        list_foo,
        list_bar
        )
如果任何类在没有定义
选项
子菜单
的情况下尝试从基本菜单继承,则实例化时将导致
类型错误
,如下所示:

import abc

class BaseMenu(object):
    __metaclass__ = abc.ABCMeta  #indicate that this is an ABC

    @abc.abstractproperty  # prevent derived classes that don't have options
    def options(self):
        pass

    @abc.abstractproperty
    def sub_menu(self):
        pass

    def __init__(self, menu_name): # takes the menu name as an argument ("server")
        self.menu_name = menu_name

    def display(self):
        header = "FooBar YO"
        term = getTerminalSize()
        print header.center(term, '*')
        print self.sub_menu.center(term, '+') # NOTE self.sub_menu
        print "Please choose which option:"
        for i in self.options: # NOTE self.options
            print(self.options.index(i)+1 + ") ")

我不确定我是否完全明白你的要求,所以告诉我,这个怎么样

TypeError: Can't instantiate abstract class Servers with abstract methods options
实例化
服务器
类,如下所示:

class BaseMenu(object):

    # Added some attributes here:
    menuName = ""
    options = ()

    def __init__(self, menu_name, opt):
        self.menuName = menu_name  # = "Servers Menu" when instantiated as Server
        self.options = opt         # the passed when instantiated as Server

    def display(self):

        # Use self.menuName and self.options here

        #...
        for i in self.options:
            print(
                str(self.options.index(i)+1) + ") " + str(i)
            )

class Servers(BaseMenu):

    def list_foo(self):
        pass
    def list_bar(self):
        pass

    options = (
        list_foo,
        list_bar
        )

    def __init__(self, menu_name):
        super(Servers, self).__init__(menu_name, self.options) 
产出:

servers = Servers("Servers Menu")
servers.display()
1)
2) 

它合适吗?

ncurses对这类事情很有帮助,不是吗?
1) <function list_foo at 0x29e06e0>
2) <function list_bar at 0x29e0758>