4 kyu

Remember members decorator

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.

  1. 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.
  2. You don't have to deal with named arguments at creating an instance of your class (such as z in A(1,z=5)).
  3. 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:

More By Author:

Check out these other kata created by zellerede

Stats:

CreatedApr 21, 2016
PublishedApr 22, 2016
Warriors Trained1124
Total Skips222
Total Code Submissions2401
Total Times Completed226
Python Completions226
Total Stars101
% of votes with a positive feedback rating95% of 74
Total "Very Satisfied" Votes68
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes1
Total Rank Assessments7
Average Assessed Rank
4 kyu
Highest Assessed Rank
2 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • zellerede Avatar
  • Voile Avatar
  • Hippunky Avatar
Ad