Repeating decorators for instance variables

故事扮演 提交于 2020-05-30 08:04:00

问题


I am writing a class where I need to check whether the instance variables are of a certain type.

I noticed that there is a lot of repeating code. Is there a better way to do similar checks on the instance variables? Or is this the right way to do it?

class Variable():

    __type = 'Variable'

    def __init__(self, id = None, updateable = True, name = 'Variable', value=None):
        if id is not None:
            self.id = id
        if value is not None:
            self.value = value
        self.updateable = updateable
        self.name = name

    @property
    def id(self):
        return self.__id

    @id.setter
    def id(self, id=None):
        if isinstance(id, int):
            self.__id = id
        else:
            raise Exception('"id" must be an integer ')

    @property
    def updateable(self):
        return self.__updateable

    @updateable.setter
    def updateable(self, updateable=None):
        if isinstance(updateable, bool):
            self.__updateable = updateable
        else:
            raise Exception('"updateatable" must be a bool')

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, name=None):
        if isinstance(name, str):
            self.__name = name
        else:
            raise Exception('"name" must be a string')

    @property
    def value(self):
        return self.__value

    @value.setter
    def value(self, value=None):
        if isinstance(value, np.ndarray):
            self.__value = value
        else:
            raise Exception('"value" not an instance of np.ndarray')

回答1:


The book Python Cookbook, 3rd Edition by Jones & Beazley contains recipe 9.21 Avoiding Repetitive Property Methods which does almost exactly what you need — I highly recommend getting a copy of the book (or e-book) to anyone interested becoming a proficient Python programmer more quickly as it contains many gems like this. (Disclaimer: I have no affiliation with the publisher or authors.)

Just about everything in Python is a first-class object, so it's possible to make a function that simply defines the property and returns it. Doing things this way will allow you to follow the DRY Principle (and write less boring redundant code).

def typed_property(name, expected_type):
    storage_name = '__' + name

    @property
    def prop(self):
        return getattr(self, storage_name)

    @prop.setter
    def prop(self, value):
        if not isinstance(value, expected_type):
            type_name = expected_type.__name__
            raise TypeError('"{}" must be a {}'.format(name, type_name))
        setattr(self, storage_name, value)

    return prop


class Variable():
    __type = 'Variable'

    id = typed_property('id', int)
    updateable = typed_property('updateable', bool)
    name = typed_property('name', str)
    value = typed_property('value', np.ndarray)

    def __init__(self, id=None, updateable=True, name='Variable', value=None):
        if id is not None:
            self.id = id
        if value is not None:
            self.value = value
        self.updateable = updateable
        self.name = name


来源:https://stackoverflow.com/questions/60895432/repeating-decorators-for-instance-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!