[Python] Create a class level decorator to automatically add a property

Discussão em 'Python' iniciado por Stack, Abril 4, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to create a class level decorator that automatically adds a property to an object, including the appropriate getter and setter methods and a backing variable. For example:

    @autoproperty("foo", can_get=True, can_set=True, allow_null=False, default_value=0)
    @autoproperty("baz", can_get=True, can_set=False, allow_null=True, default_value=0)
    @autoproperty("bar")
    class SomeNonTrivialClass(object):
    def __init__(self):
    #lots of stuff going on here

    def discombobulate(self):
    #this is obviously a very trivial example
    local_foo = self.foo;

    if (local_foo > 10):
    raise RuntimeError("Foo can never be more than 10")
    else:
    #do whatever with foo

    if __name__ == '__main__':
    test = SomeNonTrivialClass()

    test.foo = 5
    test.discombobulate()
    test.foo = 11
    test.discombobulate()


    I often find myself creating lots of "semi-complex" getters/setters (they could be done with just a simple property but they need default values and null protection. I would like to just be able to specify a decorator that does the heavy lifting of creating the properties on new instances of the class.

    If I am way off base in this approach, I am open to an equally viable approach.

    Any help would be appreciated.

    I am working with python 3.X and python 2.7 so something that works in either is preferred but not necessary.

    Update: I have added a bit more variety in what I am looking for. In general I need to be able to create a lot of these simple automatic properties (ala C# auto-property, but with a bit more flexibility). I do not necessarily want to expose the backing store, but I do want to make sure that an inspection of the instantiated object (not necessarily the class) shows the properties which have been created.

    Continue reading...

Compartilhe esta Página