简体   繁体   中英

What is the best way to change all class attributes that start with a given string?

If class attributes are set up like this, and I wanted to change all attributes that started with DBT for example, is there a way to do that without explicitly using setattr on each one individually?

class AttrTester():
    def __init__(self):
        self.DBTname = "dbt name"
        self.DBTage = "dbt age"
        self.DBTclass = "dbt class"
        self.RCCname = "rcc name"
        self.RCCage = "rcc age"
        self.RCCclass = "rcc class"
    def wipe_dbt(self):
        setattr(self, "DBTname", "None")
        setattr(self, "DBTage", "None")
        setattr(self, "DBTclass", "None")

attr = AttrTester()
attr.wipe_dbt()
print(getattr(attr, "DBTname"))
print(getattr(attr, "DBTage"))
print(getattr(attr, "DBTclass"))

Solution from comments

class AttrTester():
    def __init__(self):
        self.DBTname = "dbt name"
        self.DBTage = "dbt age"
        self.DBTclass = "dbt class"
        self.RCCname = "rcc name"
        self.RCCage = "rcc age"
        self.RCCclass = "rcc class"
    def wipe_dbt(self):
        for attrib in dir(self):
            if attrib.startswith("DBT"):
                setattr(self, attrib, "None")

attr = AttrTester()
attr.wipe_dbt()
print(getattr(attr, "DBTname"))
print(getattr(attr, "DBTage"))
print(getattr(attr, "DBTclass"))

First off, It's better to correctly say "instance attribute" because they are bound to instances(self) not class.

I assumed that what you are trying to do is to take any instance attribute starts with "DBT" and set new_value to it.

class AttrTester():
    def __init__(self):
        self.DBTname = "dbt name"
        self.DBTage = "dbt age"
        self.DBTclass = "dbt class"
        self.RCCname = "rcc name"
        self.RCCage = "rcc age"
        self.RCCclass = "rcc class"

    def wipe_dbt(self, new_value):
        for k in self.__dict__:
            if k.startswith('DBT'):
                self.__dict__[k] = new_value


attr = AttrTester()
attr.wipe_dbt(None)
print(getattr(attr, "DBTname"))
print(getattr(attr, "DBTage"))
print(getattr(attr, "DBTclass"))

Here's a fairly generic way of doing it:

class AttrTester():
    def __init__(self):
        self.DBTname = "dbt name"
        self.DBTage = "dbt age"
        self.DBTclass = "dbt class"
        self.RCCname = "rcc name"
        self.RCCage = "rcc age"
        self.RCCclass = "rcc class"

    def wipe_attr(self, prefix, value=None):
        for name, attr in self.__dict__.items():
            if name.startswith(prefix):
                setattr(self, name, value)

attr = AttrTester()
attr.wipe_attr('DB')
print(getattr(attr, "DBTname"))
print(getattr(attr, "DBTage"))
print(getattr(attr, "DBTclass"))

You could also make it a separate function if you wished:

class AttrTester():
    def __init__(self):
        self.DBTname = "dbt name"
        self.DBTage = "dbt age"
        self.DBTclass = "dbt class"
        self.RCCname = "rcc name"
        self.RCCage = "rcc age"
        self.RCCclass = "rcc class"

def wipe_attr(obj, prefix, value=None):
    for name, attr in obj.__dict__.items():
        if name.startswith(prefix):
            setattr(obj, name, value)

testobj = AttrTester()
wipe_attr(testobj, 'DB')
print(getattr(testobj, "DBTname"))
print(getattr(testobj, "DBTage"))
print(getattr(testobj, "DBTclass"))

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