Skip to content

xssChauhan/lego

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

lego

Build your classes with minimum effort

Assigning values to self in from variables passed as arguments to __init__ is generally a mundane and repeating task. Lego helps you reduce the effor to a minimum by assigning the arguments to self automagically.

Without lego

class Planet:
  def __init__(self , name , position , boom = False):
    self.name = name
    self.position = position
    self.boom = boom

>> p = Planet("Earth" , 3)
>> p.name
'Earth'
>> p.position
3
>> p.boom
False

With lego

import lego

class Planet:
  @lego.assemble
  def __init__(self , name , position , boom = False):
    pass

>> p = Planet("Earth" , 3)
>> p.name
'Earth'
>> p.position
3
>> p.boom
False

lego does not stop you from customizing the __init__ function.

class Planet:
  @lego.assemble
  def __init__(self , name , position , boom = False):
    #All the assignments have been made
    self.distance = self.position * 1000

>> p = Planet("Earth" , 3)
>> p.name
'Earth'
>> p.position
3
>> p.boom
False
>> p.distance
3000

Inspired from this SO answer

About

Minimize the effort in initializing python classes

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages