Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum number of methods on a Python class?

I am automatically generating unit tests for some Python code which number in the thousands. The unittest module uses classes to contain the tests however I'm guessing there is an upper limit to the number of methods a class may contain - is this the case?

like image 778
Brendan Avatar asked Dec 10 '22 10:12

Brendan


2 Answers

Methods (and in fact all attributes) of a class are stored in a dict. There is no limit to the number of items a dict can contain, save that each key must be unique.

like image 141
Ignacio Vazquez-Abrams Avatar answered Jan 28 '23 15:01

Ignacio Vazquez-Abrams


I strongly doubt that you'd ever hit the limit, even if there was one. As far as I know though, the number of methods an object can have is limited only by memory. I just defined a class with a million functions, with no problem. Try this if you don't believe me:

>>> class C(object): pass
>>> for i in xrange(10**6):
        exec('C.func%d=lambda self: %d'%(i,i))

>>> c = C()
>>> c.func1()
1
>>> c.func999999()
999999

If your class has more than a million functions (hell, or more than a dozen or so), you have other problems.

like image 29
Chinmay Kanchi Avatar answered Jan 28 '23 14:01

Chinmay Kanchi