Skip to main content

A minimal alternative to dataclasses and python-attrs

Project description

Logo

SelfishClass

SelfishClass is a minimal (~1kb) alternative to dataclasses and the popular attrs package for python.

SelfishClass eliminates the need to assign variables as self.varname in a class’s __init__ method and provides a simple __repr__ for the class instance.

Why not attrs?

Attrs does a lot of things very well. SelfishClass does a few things very well. Rarely do I need all of Attr’s funcionality. Attrs weighs in at ~100kb. SelfishClass is less than 1kb. In the words of the great philopsher Kevin Malone, Me think, why waste time say lot word, when few word do trick.

Why not dataclasses?

Dataclasses requires python 3.7, which may not be an option in some environments. Dataclasses require re-defining __init__ methods as __post_init__, which can feel unintuitive and may require rewriting your classes. SelfishClass only requires that you insert the decorator.

Installation

SelfishClass has been tested on Python 3.4.0 and 2.7.14 and is likely compatible with most other versions.

Install via pip with pip install SelfishClass

Usage

SelfishClass provides a simple class decorator that requires minimal or no change to your existing class definition.

from SelfishClass import selfish

@selfish
class Teacher(object):
    def __init__(self, name, id, department=None):
        pass

stan = Teacher('Stan', 445, department='Math')

print(stan)
>> Teacher: department<str>: Math, id<int>: 445, name<str>: Stan

All vars are accessible in __init__ as both instance variables and local variable. For instance, in __init__ we can refer to name as both name and self.name.

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

SelfishClass-0.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distributions

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

SelfishClass-0.1-py3-none-any.whl (2.1 kB view details)

Uploaded Python 3

SelfishClass-0.1-py2-none-any.whl (2.1 kB view details)

Uploaded Python 2

File details

Details for the file SelfishClass-0.1.tar.gz.

File metadata

  • Download URL: SelfishClass-0.1.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for SelfishClass-0.1.tar.gz
Algorithm Hash digest
SHA256 b66d5ee56f9b3e1742828d01455302d0d35e6d39c7d8672a50659c28d548f293
MD5 c0472c28ec1f4dd980d647327588f583
BLAKE2b-256 c6aff86396d80f91ef6d45e1d76fbc3695754e82ca76899e0dddba50eecfbffd

See more details on using hashes here.

File details

Details for the file SelfishClass-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for SelfishClass-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6ef7a113a72efb0db7e848d5d7f568963a27ac6f634b37c4510a08e68914f151
MD5 87b3faa2656c129d6d9383c7533765fe
BLAKE2b-256 c501b373c65d93cee23b2c423b1c7adcde31fe1d28febe941dcc63af02792b07

See more details on using hashes here.

File details

Details for the file SelfishClass-0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for SelfishClass-0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 ba332352f6c8e29c78c5ed0b72fd72e971fceb09a52dd0137a19bd77005eb272
MD5 6cb1490dd7abd534ac90ac5d650ba177
BLAKE2b-256 d43ada39505818a9e1eb06495ead3b4864457092607c0bff47bb46c2ec9020fe

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