Skip to main content

Specialized types for callable objects in Python.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

callabletype-1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

callabletype-1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file callabletype-1.0.tar.gz.

File metadata

  • Download URL: callabletype-1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for callabletype-1.0.tar.gz
Algorithm Hash digest
SHA256 0255a8cd588b72bd0591d6435cf0d5cd6f688f1d97a2a64f55834580d62e633e
MD5 3df73745fa1a7de110833f3d6ecf5228
BLAKE2b-256 2485f5b99a49c1a19a7bad2ec8b0293e6b8ae2544a97bae27b3bb87b0c37fcbc

See more details on using hashes here.

File details

Details for the file callabletype-1.0-py3-none-any.whl.

File metadata

  • Download URL: callabletype-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for callabletype-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52be3a5e1cf792be96baa461980bbb3643fb543e0399f01153673227a1255bd2
MD5 984fb762f32d4f4a9a72851c12bab5a4
BLAKE2b-256 bfb6b681910e65678a9cf1ca545fc6eb7acc6a3286040e8e05824c6bea1a5e7a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page