Skip to main content

A Python approach for class extensions like C# and Kotlin

Project description

Ext-Class

Adding fields/methods to existing classes.

A Python approach to the great extensions feature in C# and Kotlin, and probably the closest.

How to use

pip install extclass 

Suppose you have a Person class in an already installed package:

from dataclasses import dataclass

@dataclass
class Person:
    first_name: str
    last_name: str

Then, you could add an extra method outside the source package somewhere in your project.

from extclass import extension

@extension
class PersonExt(Person):
    age: int = 10

    def full_name(self):
        return f'{self.first_name} {self.last_name}'

With the help of the @extension decorator, new field age and full_name are added to the origin Person class. You can just call them as usual.

if __name__ == '__main__':
    person = Person(first_name='rick', last_name='sanchez')
    print(person.full_name())
    print(person.age)

which print as

rick sanchez
10

However, the IDE cannot know the dynamic informations of the added attributes, so there would be a warning message:

Unresolved attribute reference 'full_name' for class 'Person' 

And no autocomplete for the attributes in the IDE.

To handle the warnings also enabling autocomplete, you can simply typing.cast the origin object to the new extended type.

from typing import cast

person = cast(PersonExt, person)

In this case, person is still the old object but only marked as the extended type in order to cheat the IDE for better programming experiences.

No Override

Same method/field name in the extending class will not affect the origin class. So this package is very safe in any use-cases.

Pydantic issues

Classes of pydantic.BaseModel can only attach new methods. Dynamic fields are internally forbidden by the pydantic package. So please do NOT extend fields for pydantic models, or an AttributeError would be raised during runtime.

Full examples

See test.py

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

extclass-1.0.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file extclass-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: extclass-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.17

File hashes

Hashes for extclass-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ea727357574e52e3ba949de271dd8586c3e01e181f5a005db265840d79a9198d
MD5 75137f1ee0deca5e9c910e537ac07d76
BLAKE2b-256 b24edff439d8e319bff2740ea82ab5f21cfe75050e1dda8cc037bb31214ac028

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