如何在Python中封装类中访问上下文标签?

时间:2013-02-21 19:35:02

标签: python oop object-oriented-analysis

我正在尝试从封装类中获取封装类抓取上下文,如标签。一种方法是传递标签,就像我在下面显示的实例化一样。

我已经看到了日志记录模块如何允许您使用get_logger函数订阅子日志,并且我想要一个get_context()函数。有没有办法做到这一点?

如果这个标签的传递达到了一个级别,就像下面的YetAnotherClass一样,它似乎是可以容忍的......但是如果你必须将它传递到3或4级 - 这就是我真正想要另一个解决方案来传递这个标签的地方

我应该以不同的方式思考这个问题吗?这是一些示例代码:

class RootClass(object) :
    def __init__(self) :
        self.ac = AnotherClass('root_class_context')
    def do_insert(self) :
        """ use DataTableMgr class to update table """
        self.ac.insert_to_table('field1', 'field2', 'field3')

class AnotherClass(object) :
    def __init__(self, label) :
        self.context = label
        self.dtm = DataTableMgr('arg1','arg2', 'arg3', 'arg4', self.context)

    def insert_to_table(self, field1, field2, field3) :
        """ insert args to database using DataTableMgr """
        self.dtm.insert_to_table(field1, field2, field3)


class YetAnotherClass(object) :
    def __init__(self) :
        self.dtm = DataTableMgr('arg1','arg2', 'arg3', 'arg4', 'yetanother_context')

    def do_insert(self) :
        """ use DataTableMgr class to update table """
        self.dtm.insert_to_table('field1', 'field2', 'field3')

class DataTableMgr(object) :
    """ manage insert, updates to some table """
    def __init__(self, arg1, arg2, arg3, arg4, user_id) :
        self.context = user_id

    def insert_to_table(self, field1, field2, field3) :
        """ insert fields to table, while updating user id"""
        print( "inserting...(pretending to be updating sql database)")
        print(field1, field2, field3, self.context)
        print

if __name__ == "__main__" :
    #instantiate a class once removed from the inserting class and do insert
    rc = RootClass()
    rc.do_insert()

    #instantiate a class directly accessing the class for inserting
    yac = YetAnotherClass() 
    yac.do_insert()             

    #note how the context changes       
    print                                   
    print("Notice how the context changes?  "   
        ".. is there a better way to pass around this context information?")

1 个答案:

答案 0 :(得分:0)

嗯,我能想到的最简单的方法是使用继承:

class DataTableMgr(object):
    """ manage insert, updates to some table """
    def __init__(self, arg1, arg2, arg3, arg4, context):
        self.context = context

    def insert_to_table(self, field1, field2, field3):
        """ insert fields to table, while updating user id"""
        print("inserting...(pretending to be updating sql database)")
        print(field1, field2, field3, self.context)
        print

    def do_insert(self):
        """ use DataTableMgr class to update table """
        self.insert_to_table('field1', 'field2', 'field3')


class AnotherClass(DataTableMgr):
    def __init__(self, context):
        DataTableMgr.__init__(self, 'arg1', 'arg2', 'arg3', 'arg4', context)


class RootClass(AnotherClass):
    def __init__(self):
        AnotherClass.__init__(self, 'root_class_context')


class YetAnotherClass(DataTableMgr):
    def __init__(self):
        DataTableMgr.__init__(
            self, 'arg1', 'arg2', 'arg3', 'arg4', 'yetanother_context')

但也许您不想使用继承,因为RootClass不是DataTableMgr,而是 DataTableMgr。 在这种情况下,您可以考虑使用多态来将上下文传递给委托的封装对象。

class Base(object):
    def do_insert(self):
        """ use DataTableMgr class to update table """
        self.dtm.insert_to_table('field1', 'field2', 'field3', self.context)

    def insert_to_table(self, field1, field2, field3, context):
        """ insert args to database using DataTableMgr """
        self.dtm.insert_to_table(field1, field2, field3, context)

为了促进多态性,可以使用self.dtm访问委派对象。因此,当调用do_insert时,委派对象self.dtm会调用其insert_to_table方法。如果self.dtmDataTableMgr个实例,则会调用DataTableMgr.insert_to_table。但如果self.dtm是另一个Base实例,则会调用委托的 insert_to_table方法。依此类推,直到委托人为DataTableMgr为止。

以下是可运行代码的示例:

class Base(object):
    def do_insert(self):
        """ use DataTableMgr class to update table """
        self.dtm.insert_to_table('field1', 'field2', 'field3', self.context)

    def insert_to_table(self, field1, field2, field3, context):
        """ insert args to database using DataTableMgr """
        self.dtm.insert_to_table(field1, field2, field3, context)


class RootClass(Base):
    def __init__(self):
        self.context = 'root_class_context'
        self.dtm = AnotherClass()


class AnotherClass(Base):
    def __init__(self):
        self.dtm = DataTableMgr('arg1', 'arg2', 'arg3', 'arg4')


class YetAnotherClass(Base):
    def __init__(self):
        self.context = 'yetanother_context'
        self.dtm = DataTableMgr('arg1', 'arg2', 'arg3', 'arg4')


class DataTableMgr(object):
    """ manage insert, updates to some table """
    def __init__(self, arg1, arg2, arg3, arg4):
        pass

    def insert_to_table(self, field1, field2, field3, context):
        """ insert fields to table, while updating user id"""
        print("inserting...(pretending to be updating sql database)")
        print(field1, field2, field3, context)
        print

if __name__ == "__main__":
    # instantiate a class once removed from the inserting class and do insert
    rc = RootClass()
    rc.do_insert()

    # instantiate a class directly accessing the class for inserting
    yac = YetAnotherClass()
    yac.do_insert()

    # note how the context changes
    print
    print("Notice how the context changes?  "
          ".. is there a better way to pass around this context information?")