4 kyu
Remember members decorator
226zellerede
Description:
Write a class decorator @remember
which makes the class remember its own objects, storing them in a dictionary with the creating arguments as keys.
Also, you have to avoid creating a new member of the class with the same initial arguments as a previously remembered member.
Additionally, if the (decorated) class is A
, you will have to reach that dictionary of remembered objects directly on A
, i.e. by A[args]
and by the loop for x in A
over the keys.
Example
A sample usage:
@remember
class A(object):
def __init__(self, x,y=0,z=0):
pass
a = A(1)
b = A(2,3)
c = A(4,5,6)
d = A(1)
>>> A[1] is a is d
True
>>> A[2,3] is b
True
>>> A[4,5,6] is c
True
>>> for x in A: print x,
(2,3) (4,5,6) 1
Notes.
- Other dict methods like
items()
,keys()
, etc. are nice to have but not required to be implemented on the decorated class itself for this kata. - You don't have to deal with named arguments at creating an instance of your class (such as
z
inA(1,z=5)
). - If the constructor is called with a single argument, the argument itself will be the key and not its 1-tuple.
Decorator
Singleton
Similar Kata:
Stats:
Created | Apr 21, 2016 |
Published | Apr 22, 2016 |
Warriors Trained | 1124 |
Total Skips | 222 |
Total Code Submissions | 2401 |
Total Times Completed | 226 |
Python Completions | 226 |
Total Stars | 101 |
% of votes with a positive feedback rating | 95% of 74 |
Total "Very Satisfied" Votes | 68 |
Total "Somewhat Satisfied" Votes | 5 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 7 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 2 kyu |
Lowest Assessed Rank | 6 kyu |