A new Object Oriented System for Python (Kisa)

Hello everyone,

I am a Python programmer for quite a while and although Python makes development easier and faster in a lot of ways, there is an area that always seemed that was kept behind even though it was widely used by the Python community, and that thing I’m talking about is Pythons native Object Oriented System.

While Java or C# or even C++ has an Object Oriented system that support attribute type enforcement, interfaces and abstract classes, Python can somewhat provide those things, but without any enforcement at all.

That always seemed to me a shameful thing, because Python in many ways is a very powerful language, but that also makes it prone to errors. For instance, I’ve had many bugs in Python when I was trying to assign a value to the wrong attribute because I simply misspelled it’s name by one character (e.g. namee instead of name).

Another issue that seemed problematic to me is that there are a lot of repetitive tasks we do when creating classes that could be automated as well, from constructors, type enforcement, lazy attributes to the creation of getters/setters (While I’m aware of the @property decorator, I always preferred get/set methods). Granted, Python 3.7 introduced the @dataclass classes, they are very simplistic — No type enforcement, default values assignement is basic at best (You cannot call a method to assign their default values), let alone speak about lazy attributes, or final attributes.

For that reason, I’ve decided to create a new Object Oriented System for Python — Kisa. Kisa supposed to bring many improvements over Pythons native Object Oriented System and that includes:

  • Auto generated getters/setters
  • Auto generated constructors
  • Default attributes — Can be initialized from instance method
  • Lazy attributes
  • Attribute type support
  • Abstract classes
  • Interfaces
  • Enforcement of inheritance
  • Static attributes
  • Static methods
  • Attribute modifiers — Add hooks to attribute/method call before the call, around the call or after it was called
  • Allow attributes to be None or not
  • Make attributes final
  • Decide if attributes are required by the constructor or not
  • Many more

I believe Kisa can and will make development in Python faster to write, maintain, better organized, and safer.

A very basic example of a Kisa class:

import kisa

class Person(metaclass=kisa.Class):
    firstname = kisa.Info(type=str,  required=True, final=True)
    lastname = kisa.Info(type="str", required=True, final=True)
    age = kisa.Info(type=int, required=True)
    friends = kisa.Info(type=list, default=lambda: [])

    def fullname(self):
        return f"{self.firstname()} {self.lastname()}"

    def birthday(self):
        # Getter
        new_age = self.age() + 1

        # Setter
        self.age(new_age)

# # raises Exception: "firstname" is Missing in instance creation for class Person
# noam = Person()

noam = Person(firstname="Noam", lastname="Nisanov", age=22)

# raises Exception: Tried to modify a final attribute "firstname"
# noam.firstname("Generic")

# raises Exception: "age" must be of type: <class 'int'>
# noam.age("bla bla")

print(f"First name: {noam.firstname()}") # prints "First name: Noam"
print(f"Last name: {noam.lastname()}") # prints "Last name: Nisanov"
print(f"Full name: {noam.fullname()}") # prints "Full name: Noam Nisanov"

print(f"Current age: {noam.age()}") # prints "Current age: 22"
print(f"Birthday!") # prints "Birthday!"
noam.birthday()
print(f"New age: {noam.age()}") # prints "New age: 23"

For more information, please take a look at the project GitHub
https://github.com/nmnsnv/kisa

Thanks
Noam Nisanov (noam.nisanov@gmail.com)

This category is about the tools that allow distributing Python projects, so I believe you are not reaching the audience you would like. The Users category might be more appropriate.

Moved to Help/Users.

3 Likes