Skip to main content

Simplify your __init__(self) function.

Project description

The missing magical python decorator for simplifying your __init__(self, ...) function.

@init_args # Magical decorator
def __init__(self, name="Lovely Lucy", age=10):
    pass

p = Person()
p.name # "Lovely Lucy"
p.age  # 10 **Sweet**!

Inherited variables from super class

If you have polymorphism structured classes, you’d love initify.

class Animal:
    def __init__(self, obj):  # REQUIRED reserve "obj" for super class injecting
        self.age = 0
        self.name = ""
        self.color = ""
        ...

class Dog(Animal):
    @init_args
    def __init__(self, pet=True):
        pass
    ...

class Cat(Animal):
    @init_args
    def __init__(self, wild=False):
        pass
    ...
d = Dog(age=3, name="Golfy", color="black")
d.age    # 3
d.name   # Golfy
d.color  # black
d.pet    # True

c = Cat()
c.age   # 0
c.name  # ""
c.color # ""
c.wild  # False

That’s dull :/ Don’t judge until you see this:

Exclused variables

# Must declare with new-style class in python 2.7 or earlier
# ``class Animal:`` is fine if using python 3 or later
class Animal(object):
    def __init__(self):
        self.age = 0
        self.name = ""
        self.color = ""
        ...
        self.barks = True  # Only dog barks
        self.meows = True  # and only cat meows
        ...

class Dog(Animal):
    @init_args(exclude=['meows'])  # Don't inherit meows from super class!
    def __init__(self, pet=True):
        pass
    ...

class Cat(Animal):
    @init_args(exclude=['barks'])  # Don't inherit barks from super class!
    def __init__(self, wild=False):
        pass
    ...
d = Dog(age=3, name="Golfy", color="black")
d.age    # 3
d.name   # Golfy
d.color  # black
d.pet    # True
...
d.barks  # True
d.meows  # Attribute Error: meows is not defined!

c = Cat()
c.age   # 0
c.name  # ""
c.color # ""
c.wild  # False
...
d.meows  # True
d.barks  # Attribute Error: barks is not defined!

Now this is neat hey?

Installation

Install initify with pip or pip3.

pip install initify
pip3 install initify

Then whichever class you’re using initify, do an import before using. Example:

from initify import init_args

Now you can attach the decorator @init_args right before the class’s initializer def ___init___(self, ...). Enjoy your effort.

Support

Hope this decorator can help you with your `DRY-iness in your python project!

If you have any suggestion, please don’t hesistate to give a post on the issue page.

Cheers and happy hacking!

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

initify-0.1.4.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

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

initify-0.1.4-py2-none-any.whl (2.4 kB view details)

Uploaded Python 2

File details

Details for the file initify-0.1.4.tar.gz.

File metadata

  • Download URL: initify-0.1.4.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for initify-0.1.4.tar.gz
Algorithm Hash digest
SHA256 38c9076b7a06763ce1a4968c38126394736ebabc08eff78cea14c9585bed70f5
MD5 3a28b227ca08735344f5865f7ac1a5fe
BLAKE2b-256 7e9c5e8927b8284d57ba5c3bd83d6e83ca4fa26b058ded6496acedd5cb9e76b3

See more details on using hashes here.

File details

Details for the file initify-0.1.4-py2-none-any.whl.

File metadata

File hashes

Hashes for initify-0.1.4-py2-none-any.whl
Algorithm Hash digest
SHA256 696da75e363abfd66ece3b95726fe6e20e88ac4fbf2289836b3c71ca702be001
MD5 2775358981a3fa95d3af20e5c371d06e
BLAKE2b-256 833e41d8fd44a6877453fb38bb113f37a3a4ece356cf86dd108221fc6b822db6

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