简体   繁体   English

在类方法中将实例变量作为参数传递?

[英]Passing in an instance variable as an argument in a class method?

I'm trying to refactor a very repetitive section of code. 我正在尝试重构代码的非常重复的部分。

I have a class that has two instance variables that get updated: 我有一类具有两个实例变量的更新:

class Alerter(object):

    'Sends email regarding information about unmapped positions and trades'

    def __init__(self, job):
        self.job = job
        self.unmappedPositions = None
        self.unmappedTrades = None

After my code going through some methods, it creates a table and updates self.unmappedPositions and self.unmappedTrades : 在我的代码经过一些方法之后,它创建了一个表并更新了self.unmappedPositionsself.unmappedTrades

def load_positions(self, filename):
    unmapped_positions_table = etl.fromcsv(filename)
    if 'positions' in filename:
        return self.add_to_unmapped_positions(unmapped_positions_table)
    else:
        return self.add_to_unmapped_trades(unmapped_positions_table)

So I have two functions that essentially do the same thing: 因此,我有两个功能基本相同:

def add_to_unmapped_trades(self, table):
    if self.unmappedTrades:
        Logger.info("Adding to unmapped")
        self.unmappedTrades = self.unmappedTrades.cat(
            table).cache()
    else:
        Logger.info("Making new unmapped")
        self.unmappedTrades = table
    Logger.info("Data added to unmapped")
    return self.unmappedTrades

And: 和:

def add_to_unmapped_positions(self, table):
    if self.unmappedPositions:
        Logger.info("Adding to unmapped")
        self.unmappedPositions = self.unmappedPositions.cat(
            table).cache()
    else:
        Logger.info("Making new unmapped")
        self.unmappedPositions = table
    Logger.info("Data added to unmapped")
    return self.unmappedPositions

I tried making it one method so that it just passes in a third argument and then figures out what to update. 我尝试将其设置为一种方法,以便仅传递第三个参数,然后找出要更新的内容。 The third argument being the intialized variable, either self.unmappedPositions or self.unmappedTrades . 第三个参数是初始化变量,可以是self.unmappedPositionsself.unmappedTrades However, that doesn't seem to work. 但是,这似乎不起作用。 Any other suggestions? 还有其他建议吗?

It looks like you've had the key insight that you can write this function independent of any particular storage: 您似乎已经掌握了关键的见解,可以独立于任何特定的存储编写此函数:

def add_to_unmapped(unmapped, table):
    if unmapped:
        Logger.info("Adding to unmapped")
        unmapped = unmapped.cat(table).cache()
    else:
        Logger.info("Making new unmapped")
        unmapped = table
    Logger.info("Data added to unmapped")
    return unmapped

This is actually good practice on its own. 实际上,这本身就是一种好习惯。 For instance, you can write unit tests for it, or if you have two tables (as you do) you can just write the implementation for it once. 例如,您可以为其编写单元测试,或者,如果您有两个表(如您所愿),则只需为其编写一次实现。

If you consider what, abstractly, your two add_to_unmapped_* functions do, they: 如果您考虑一下您的两个add_to_unmapped_*函数的作用,它们是:

  1. Compute the new table; 计算新表;
  2. Save the new table in the object; 将新表保存在对象中; and
  3. Return the new table. 返回新表。

We've now separated out step 1, and you can refactor the wrappers: 现在,我们将步骤1分离出来,您可以重构包装器:

class Alerter:
    def add_to_unmapped_trades(self, table):
        self.unmappedTrades = add_to_unmapped(self.unmappedTrades, table)
        return self.unmappedTrades

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

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