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 functionality. Attrs weighs in at ~100kb. SelfishClass is less than 1kb. In the words of the great philosopher 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.21.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.21-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

SelfishClass-0.21-py2-none-any.whl (2.3 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: SelfishClass-0.21.tar.gz
  • Upload date:
  • Size: 2.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.4

File hashes

Hashes for SelfishClass-0.21.tar.gz
Algorithm Hash digest
SHA256 6a26a16e7f6419bca304750fdea71ab825173b8841b49db7e42a8d0c026dadcf
MD5 8184e9d76a6b301bf7b3feebfecb58a8
BLAKE2b-256 3be16ee6262e28e117819995477d99c9707b3be2cd532454bf19e936eecd12ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: SelfishClass-0.21-py3-none-any.whl
  • Upload date:
  • Size: 2.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.4

File hashes

Hashes for SelfishClass-0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 4680a82d95a179ab3ba5d5f45df2c0e6798d4087d39b2b22239b3d7602816670
MD5 d68c1b96dc4638355785714b84f39a57
BLAKE2b-256 e18623b498d187a84dff0ab73805146bc34e89312fce36ee4ea7bbfc1467d74e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: SelfishClass-0.21-py2-none-any.whl
  • Upload date:
  • Size: 2.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/18.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.4.4

File hashes

Hashes for SelfishClass-0.21-py2-none-any.whl
Algorithm Hash digest
SHA256 aa76ad1383609e9c4128483a5cdf35e062c93c1de00bb9834e980c47bc83d686
MD5 ed63739db01353c8713cf9fd1c8979fc
BLAKE2b-256 f731fc7e124e3e224f89bb0f3c37549dfafd7f8ab566650ec49c3f6404133efa

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