mxndtaylor / aliasing

a small utility library to add aliases to python classes
MIT License
1 stars 0 forks source link

Feature: allow calling `attach` on an instance of a class without modifying the class itself #24

Closed mxndtaylor closed 4 weeks ago

mxndtaylor commented 4 weeks ago

Is your feature request related to a problem? Please describe. It's a little frustrating that if you call alias.attach on an instance variable, the object's class will be modified with the alias. For instance:

import aliasing as aka

class Foo:
    def method(self):
        return "baz"

bar = aka.alias(alias_for="method", alias_name="bar")
f1 = Foo()

bar.attach(f1)
# prints "True"
print(f1.bar() == f1.method())

f2 = Foo()
# prints "True"
print(f2.bar() == f1.method())

Describe the solution you'd like I'd like to see this behavior instead:

import aliasing as aka

class Foo:
    def method(self):
        return "baz"

bar = aka.alias(alias_for="method", alias_name="bar")
f1 = Foo()

bar.attach(f1)
# prints "True"
print(f1.bar() == f1.method())

f2 = Foo()
# raises AttributeError
print(f2.bar() == f1.method())