Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Contributors Forks Stargazers Issues MIT License LinkedIn

Python CallableType

Specialized types for callable objects in Python.

Changelog · Report Bug · Request Feature

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.

Built With

  • python-shield

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

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


License

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


Contact

Tahsin Çirkin - @TahsinCrs - TahsinCr@outlook.com

Project: TahsinCr/python-callabletype

Release files for callabletype 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for callabletype 1.0.1
File Size Uploaded
callabletype-1.0.1.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for callabletype 1.0.1
File Interpreter ABI Platform
callabletype-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / callabletype-1.0.1.tar.gz

Download URL callabletype-1.0.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c71feb20ecb370d3843e2722e0472644ee205f4e6d01ebbc148ca2d6f65e9827
BLAKE2b-256 checksum
How to use checksums
53b41dec83fc283a01ae939385c030a5be07b914ec7e4d4c9787fbb8b0efdfa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / callabletype-1.0.1-py3-none-any.whl

Download URL callabletype-1.0.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b00a30bb9808f93cb900e5d81dcacb23cca0c7f138434eef5da6fe36f4d34745
BLAKE2b-256 checksum
How to use checksums
3c6d526d96aa65a60d5659432f1cbeafad7cbf51f766bddfcf1a1f522bb1fc55
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page