Skip to content

Delegate properties on a class to the attributes of one of its members

License

Notifications You must be signed in to change notification settings

dscottboggs/python-delegate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Delegate

A python library for delegation (the metaprogramming feature)

This library adds the @delegate decorator which may be used to delegate attributes from an attribute of the existing class. For example:

from delegate import delegate

class Parent:
    def __init__(self):
        self.a = "a"
        self.b = "b"
        self.d = "d"

# The delegate decorator makes .a and .b available on Child, through its
# "parent" attribute, as though Child had an a and b attribute itself.
@delegate("a", "b", to="parent")
class Child:
    def __init__(self):
        self.parent = Parent()
        self.c = "c"

instance = Child()
assert instance.a == "a"
raised = False
try:
    # But d is not available
    instance.d
except e:
    raised = True

assert raised

About

Delegate properties on a class to the attributes of one of its members

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages