Giter VIP home page Giter VIP logo

overloaded's Introduction

overloaded

Polymorphism in python! May overload functions and methods (including static- and classmethods). Supports complex type hinting. Does not modify original functions, classes and methods.

All you need is an instance of Overloader:

overloaded = Overloader()

Examples:

With functions:

@overloaded('useless') 
def foo(): return 0

@overloaded('adder') # NOTE You may use as ID any hashable type except classes and functions
def foo(a, b): return a + b

@overloaded
def foo(a: str, b: str): return '...'.join((a, b))

@overloaded
def foo(a: float, b: int): return int(a) + b

assert overloaded.foo() == 0
assert overloaded.foo(2, 2) == 4
assert overloaded.foo('me', 'ow') == 'me...ow'
assert overloaded.foo(2.5, 8) == 10

assert overloaded.foo.with_id('useless')() == 0
assert overloaded.foo.with_id('adder')(5, 5) == 10

With methods:

@overloaded
class A:
    hidden = 42

    @overloaded.method('simple-method')
    def foo(self): return 'normal_foo_' + str(self.hidden)

    @overloaded.method
    @classmethod
    def bar(cls): return 'classmethod_bar_' + str(cls.hidden)

    @overloaded.method
    @staticmethod
    def baz(): return 'staticmethod_baz'

    @overloaded.method
    @staticmethod
    def sum(*args): return sum(args)

a = A()
a.hidden = 13

assert overloaded.A.foo(a) == 'normal_foo_13'
assert overloaded.A.foo.with_id('simple-method')(a)

# That's how classmethod works
assert overloaded.A.bar(A) == 'classmethod_bar_42' # Class
assert overloaded.A.bar(a) == 'classmethod_bar_42' # Instance

assert overloaded.A.baz() == 'staticmethod_baz'
assert overloaded.A.sum(1, 2, 3, 4) == 10

Install:

pip install git+https://github.com/phantie/overloaded.git -U

Note.

In situation where at least 2 functions/methods can be executed with given arguments and amount of hints you provide is equal among them you may define more specific ones at the first place, to get a desirable behaviour.

def foo(a: Real, b: Real): ...
def foo(a: Number, b: Number): ...

overloaded's People

Contributors

phantie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.