0
class object_restrict(object): _count = 0 def __new__(cls): if cls._count > 5: raise TypeError("Too many keys created") cls._count += 1 print "object created" def __init__(self): pass k = object_restrict() k1 = object_restrict() k2 = object_restrict() k3 = object_restrict() k4 = object_restrict() k5 = object_restrict() 

It seems I have some questions regarding how can we restrict the number of objects for a class in Python. I have been asked to write a program where I should put the condition where we can create only 5 instances of a class, and if we try to create more than 5, it should raise an exception.

As we know in Python, __new__ is the method which is get called whenever an instance needs to be created. I tried to write some code, but it didn't work.

When I ran this code, it ran for all 6 times. Please can somebody guide me here? I also tried checking on Google but didn't get any proper code.

4
  • 3
    Post your code as code, not as a picture. Commented Apr 10, 2017 at 13:18
  • 1
    Try creating a 7th object. Commented Apr 10, 2017 at 13:21
  • i a trying to post the code but it giving me error so again posted as image Commented Apr 10, 2017 at 13:30
  • I answered to your question. Commented Apr 10, 2017 at 13:33

4 Answers 4

1

as pointed out by Wilfred there's a problem with the _count check or variable initialization. but i would like to point out another problem which is that you are not returning the instance of the object. if you have this:

class A(object): _count = 0 def __new__(cls): if cls._count > 5: raise Exception('Too many instances were created') cls._count += 1 def __init__(self): pass asd = A() print asd 

will output:

$ None 

you should return the instance of the object, which is actually __new__ responsibility:

class A(object): _count = 0 def __new__(cls): if cls._count >= 5: # @Wilfred fix raise Exception('Too many instances were created') cls._count += 1 return super(A, cls).__new__(cls) # returning the instance def __init__(self): pass asd = A() print asd 

output:

$ <__main__.A object at 0x7f5ddad7db10> 
Sign up to request clarification or add additional context in comments.

Comments

1
class object_restrict(object): _count = 0 def __new__(cls): cls._count += 1 if cls._count > 5: raise TypeError("Too many keys created") print cls._count, "object created" def __init__(self): pass k = object_restrict() k1 = object_restrict() k2 = object_restrict() k3 = object_restrict() k4 = object_restrict() k5 = object_restrict() 

Comments

0

You init your _count variable to 0.

First call : _count = 0

Sixth call : _count = 5, So he can create your object.

Init _count = 1 or update your if condition to cls._count >= 5:

1 Comment

Glad to help you. Don't forget to accept this anwser ;)
0

A class that allows maximum 5 objects to be instantiated (Python 3):

class RestrictClass(object): __count = 0 def __new__(cls): if cls.__count>=5: raise TypeError("Already instantiated") cls.__count+=1 return super(RestrictClass,cls).__new__(cls) def __init__(self): pass def showNumberOfObjects(self): print("Number of objects are now: "+str(self.__count)) k1 = RestrictClass() k1.showNumberOfObjects() k2 = RestrictClass() k2.showNumberOfObjects() k3 = RestrictClass() k3.showNumberOfObjects() k4 = RestrictClass() k4.showNumberOfObjects() k5 = RestrictClass() k5.showNumberOfObjects() k6 = RestrictClass() k6.showNumberOfObjects() 

Output:

Number of objects are now: 1 Number of objects are now: 2 Number of objects are now: 3 Number of objects are now: 4 Number of objects are now: 5 Traceback (most recent call last): File "/media/arsho/Documents/pyPrac/object.py", line 27, in <module> k6 = RestrictClass() File "/media/arsho/Documents/pyPrac/object.py", line 5, in __new__ raise TypeError("Already instantiated") TypeError: Already instantiated 

1 Comment

there's no need for the else clause.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.