A minimal alternative to dataclasses and python-attrs
Project description
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 Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file SelfishClass-0.3-py3-none-any.whl.
File metadata
- Download URL: SelfishClass-0.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6fe3860293bd3bbe4c0ca224e5e0600e9c9d66a5d19bda9ada439fa2df44c572
|
|
| MD5 |
baf429f16d6a41541435de6b58856869
|
|
| BLAKE2b-256 |
41403cad195591e96e025c69755a8459b45d4d192782b74fe76b8cae4f5da2a8
|
File details
Details for the file SelfishClass-0.3-py2-none-any.whl.
File metadata
- Download URL: SelfishClass-0.3-py2-none-any.whl
- Upload date:
- Size: 2.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85ce21f013e7a9af76b202a92d1a48bd1ed38a93f23a34f1e023178dedbe367c
|
|
| MD5 |
6a5ed62a61c87c30fb1806af830f5d1f
|
|
| BLAKE2b-256 |
83c03615a2b39f3ff17dfe00f6cb2aec4a5cf7fea63f9d09c0339f065323e247
|