Skip to content

TahsinCr/python-callabletype

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn

Logo Logo


Logo

Python CallableType

Specialized types for callable objects in Python.

Changelog · Report Bug · Request Feature

Table of Contents
  1. About The Project

  2. Getting Started

  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

Python 3.x provides customized callable types.

How can we distinguish between types like class, function, and method? You might think of using the types or inspect modules as a solution. However, these traditional types can be a bit confusing. Especially when it comes to distinguishing between function and method types, you'll notice that types.FunctionType does not cover types.MethodType. For this reason, callable types have been redefined using this library.

(back to top)

Built With

  • python-shield

(back to top)

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

Does not require any prerequisites

Installation

  1. Clone the repo
git clone https://github.com/TahsinCr/python-callabletype.git
  1. Install PIP packages
pip install callabletype

(back to top)

Usage

We have a class named Parrot(name, color, age) that includes a method walk(self). Additionally, there is a function called parrot_fly(). We can choose and control the types of these functions in detail.

from callabletype import (
    get_callable_type, is_function, CLASS, FUNCTION, METHOD, SELFMETHOD, INSTANCESELFMETHOD
)

class Parrot:
    def __init__(self, name:str, color:str, age:int):
        self.name = name
        self.color = color
        self.age = age
    
    def walk(self):
        print("The parrot named {} walked".format(self.name))
    
def parrot_fly(parrot:Parrot):
    print("The parrot named {} flew".format(parrot.name))

parrot = Parrot(name='Kiwi', color='green', age=14)

objects = {
    'function' : parrot_fly,
    'class' : Parrot,
    'method' : Parrot.walk,
    'self-method' : Parrot.walk,
    'instance-self-method' : parrot.walk
}

for type_name, object in objects.items():
    type = get_callable_type(object)
    check_function = is_function(object)
    check_method = type in METHOD
    check_class = CLASS.check_func(object)
    print("'{}' : {} (result_type={}, is_function={}, is_method={}, is_class={})\n".format(
        object, type_name, type, check_function, check_method, check_class
    ))

Output

'<function parrot_fly at 0x000001D8C23F3EC0>' : function (result_type=FUNCTION, is_function=True, is_method=False, is_class=False)

'<class '__main__.Parrot'>' : class (result_type=CLASS, is_function=False, is_method=False, is_class=True)

'<function Parrot.walk at 0x000001D8C25B1260>' : method (result_type=SELFMETHOD, is_function=True, is_method=True, is_class=False)

'<function Parrot.walk at 0x000001D8C25B1260>' : self-method (result_type=SELFMETHOD, is_function=True, is_method=True, is_class=False)

'<bound method Parrot.walk of <__main__.Parrot object at 0x000001D8C2455BE0>>' : instance-self-method (result_type=INSTANCESELFMETHOD, is_function=True, is_method=True, is_class=False)

For more examples, please refer to the Documentation

(back to top)

Roadmap

  • Add documentation.

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

Don't forget to give the project a star! Thanks again!

  1. Fork the Project

  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)

  3. Commit your Changes (git commit -m 'Add some AmazingFeature')

  4. Push to the Branch (git push origin feature/AmazingFeature)

  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Email: TahsinCr@outlook.com

X: @TahsinCrs

LinkedIn: TahsinCr

(back to top)

Acknowledgments

  • PyPI

  • [C-Sharp Linq Documents][dotnet-lib-linq]

(back to top)

About

Argument classes for callable objects in Python.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages