简体   繁体   中英

“Routine” function before the call of a function in python

World!

I'm creating a class in python called FBST .It has the following attributes:

class FBST:

    def __init__(self, x, verbose=False):

        self._rejected_met = []
        self._acceptance_met = []
        self._acceptance_bar = []
        self._rejected_bar = []
        self.x = x
        self.verbose = verbose

As you can see, it has 4 diferent lists. Those 4 lists store information about a function called _chain() . Each time I call the chain() method, have to "clean" those 4 lists, turning each one of them in to a empty list again. I'm using the following code to make this cleaning process.


def chain(self):

    #cleaning the attributes
    for att in dir(self):
        if att.startswith("_"):
            setattr(self, att, [])

Is this there a more "pythonic" approach to this loop in the beggining of the function?

Rather than depending on a specific naming convention for the variables, put them in a dictionary.

class FBST:

    def __init__(self, x, verbose=False):
        self.acc_rej = {
            "rejected_met": [],
            "accepance_met": [],
            "rejected_bar": [],
            "acceptance_bar": []
        }
        self.x = x
        self.verbose = verbose

    def chain(self):
        for key in self.acc_rej:
            self.acc_rej[key] = []
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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