Skip to content

Commit 71dc88d

Browse files
committed
solve(codewars): Lazy Init [4 kyu]
1 parent 59d916a commit 71dc88d

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Lazy Init
2+
3+
How many times we create a python class and in the init method we just write:
4+
5+
```
6+
self.name1 = name1
7+
self.name2 = name2
8+
.....
9+
```
10+
11+
for all arguments.....
12+
13+
How boring!!!!
14+
15+
Your task here is to implement a metaclass that let this instantiation be done automatically. But let's see an example:
16+
```
17+
class Person(metaclass=LazyInit):
18+
def __init__(self, name, age): pass
19+
```
20+
When we create a Person object like:
21+
```
22+
a_person = Person('John', 25)
23+
```
24+
The expected behavior will be:
25+
```
26+
print(a_person.name) # this will print John
27+
print(a_person.age) # this will print 25
28+
```
29+
30+
Obviously the number of arguments might be different from class to class.
31+
32+
Don't worry about **kwargs you will never be given keyword arguments in this kata!!!
33+
34+
A little hint: a decorator could help you out doing the job!!!
35+
36+
Good luck lazy folks.....
37+
38+
[Kata link](https://www.codewars.com/kata/59b7b43b4f98a81b2d00000a/)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import inspect
2+
3+
4+
class LazyInit(type):
5+
def __new__(cls, name, bases, attrs):
6+
if init_method := attrs.get('__init__'):
7+
init_parameters = inspect.signature(init_method).parameters
8+
param_names = [name for name, param in init_parameters.items() if name != 'self']
9+
10+
def lazy_init(self, *args, **kwargs):
11+
# TODO maybe write for kwargs too (although this is not provided for by the assignment)
12+
for i, param_name in enumerate(param_names):
13+
setattr(self, param_name, args[i])
14+
15+
attrs['__init__'] = lazy_init
16+
17+
return super().__new__(cls, name, bases, attrs)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from solution_lazy_init import LazyInit
2+
3+
4+
def test_lazy_init():
5+
class Person(metaclass=LazyInit):
6+
def __init__(self, name, age): pass
7+
8+
class Circle(metaclass=LazyInit):
9+
def __init__(self, x, y, ray): pass
10+
11+
a_person = Person('Luke', 21)
12+
13+
assert a_person.name == 'Luke'
14+
assert a_person.age == 21
15+
16+
a_circle = Circle(0, 0, 5)
17+
assert a_circle.x == 0
18+
assert a_circle.y == 0
19+
assert a_circle.ray == 5
20+
21+
class Car(metaclass=LazyInit):
22+
def __init__(self, model, color, plate, year): pass
23+
24+
class Nothing(metaclass=LazyInit):
25+
def __init__(self, nothing): pass
26+
27+
a_car = Car('Ford Ka', 'Blue', 'AF329SZ', 1999)
28+
a_nothing = Nothing('nothing')
29+
30+
assert a_car.model == 'Ford Ka'
31+
assert a_car.color == 'Blue'
32+
assert a_car.plate == 'AF329SZ'
33+
assert a_car.year == 1999
34+
assert a_nothing.nothing == 'nothing'

0 commit comments

Comments
 (0)