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.

You may specify certain arguments to ignore if you do not want them to be assigned as instance vars. This is accomplished by using the decorator as such:

# All positional args will be ignored by selfish
@selfish(args=False)
class Teacher(object):
    def __init__(self, name, id, department=None):
        pass

# All kwargs will be ignored by selfish
@selfish(kwargs=False)
class Teacher(object):
    def __init__(self, name, id, department=None):
        pass

# Only id will be ignored by selfish
@selfish(ignore=['id'])
class Teacher(object):
    def __init__(self, name, id, department=None):
        pass

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.2.tar.gz (2.3 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.2-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

SelfishClass-0.2-py2-none-any.whl (2.2 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for SelfishClass-0.2.tar.gz
Algorithm Hash digest
SHA256 1f81fc98a6141329d4d2b567782731bc42831930183f6174e7699875f13dd327
MD5 cdb29748e614cdbc91386c45df573301
BLAKE2b-256 04a6dcfa033ebb562cb26952243f4b40038add837186edf848f6a64a64202bf0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for SelfishClass-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fad6ee291c5a7fbdf428a7ec96bc75bcb1226a5b7dba27970db9f6f243759ede
MD5 088be2f2c3939f8230fd9839540b0be5
BLAKE2b-256 37c6621f8186bb76599af763ca3d28923a96db7700bdc0cfa5b87565ddbd04ce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for SelfishClass-0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 8558ddd327ca4cb3970f616303e14dd821c17db3118cf660396bce88550c086d
MD5 dba98765ed77776fd739f6f3b3f9f5a7
BLAKE2b-256 40a40db3d4c1d9022cb1736f7d6a1a8d590fff3554088915631dae7af624d5a7

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