Skip to content

howShouldIChooseMyUsername/weakProperty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

weakProperty

just a property, but it automatically got weakref

source code :

def weakProperty(func):
    attr_name = f"_weak_{func.__name__}"
    @property
    def wrapper(self):
        ref = getattr(self, attr_name, None)
        return None if ref is None else ref()

    @wrapper.setter
    def wrapper(self, value):
        if value is None:
            setattr(self, attr_name, None)
        else:
            setattr(self, attr_name, weakref.ref(value))

    @wrapper.deleter
    def wrapper(self):
        setattr(self, attr_name, None)

    return wrapper

usage

same as @property

About

weakProperty

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages