<p>I'm teaching myself Python and my most recent lesson was that <a href="http://dirtsimple.org/2004/12/python-is-not-java.html" rel="noreferrer">Python is not Java</a>, and so I've just spent a while turning all my Class methods into functions.</p>

<p>I now realise that I don't need to use Class methods for what I would done with <code>static</code> methods in Java, but now I'm not sure when I would use them.  All the advice I can find about Python Class methods is along the lines of newbies like me should steer clear of them, and the standard documentation is at its most opaque when discussing them.</p>

<p>Does anyone have a good example of using a Class method in Python or at least can someone tell me when Class methods can be sensibly used?</p>

[{'answer_id': 38254, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 5, 'selected': False, 'text': '<p>Alternative constructors are the classic example.</p>\n'}, {'answer_id': 38276, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 9, 'selected': True, 'text': "<p>Class methods are for when you need to have methods that aren't specific to any particular instance, but still involve the class in some way. The most interesting thing about them is that they can be overridden by subclasses, something that's simply not possible in Java's static methods or Python's module-level functions.</p>\n\n<p>If you have a class <code>MyClass</code>, and a module-level function that operates on MyClass (factory, dependency injection stub, etc), make it a <code>classmethod</code>. Then it'll be available to subclasses.</p>\n"}, {'answer_id': 38287, 'author': 'Jason Baker', 'author_id': 2147, 'author_profile': 'https://Stackoverflow.com/users/2147', 'pm_score': 3, 'selected': False, 'text': "<p>Honestly?  I've never found a use for staticmethod or classmethod.  I've yet to see an operation that can't be done using a global function or an instance method.</p>\n\n<p>It would be different if python used private and protected members more like Java does.  In Java, I need a static method to be able to access an instance's private members to do stuff.  In Python, that's rarely necessary.</p>\n\n<p>Usually, I see people using staticmethods and classmethods when all they really need to do is use python's module-level namespaces better.</p>\n"}, {'answer_id': 38303, 'author': 'Wayne Werner', 'author_id': 4080, 'author_profile': 'https://Stackoverflow.com/users/4080', 'pm_score': 6, 'selected': False, 'text': '<p>Factory methods (alternative constructors) are indeed a classic example of class methods.</p>\n\n<p>Basically, class methods are suitable anytime you would like to have a method which naturally fits into the namespace of the class, but is not associated with a particular instance of the class.</p>\n\n<p>As an example, in the excellent <a href="http://pypi.python.org/pypi/Unipath/0.2.1" rel="noreferrer">unipath</a> module:</p>\n\n<h2>Current directory</h2>\n\n<ul>\n<li><code>Path.cwd()</code>\n\n<ul>\n<li>Return the actual current directory; e.g., <code>Path("/tmp/my_temp_dir")</code>. This is a class method.</li>\n</ul></li>\n<li><code>.chdir()</code>\n\n<ul>\n<li>Make self the current directory.</li>\n</ul></li>\n</ul>\n\n<p>As the current directory is process wide, the <code>cwd</code> method has no particular instance with which it should be associated.  However, changing the <code>cwd</code> to the directory of a given <code>Path</code> instance should indeed be an instance method.</p>\n\n<p>Hmmm... as <code>Path.cwd()</code> does indeed return a <code>Path</code> instance, I guess it could be considered to be a factory method...</p>\n'}, {'answer_id': 2311096, 'author': 'Drachenfels', 'author_id': 278709, 'author_profile': 'https://Stackoverflow.com/users/278709', 'pm_score': 3, 'selected': False, 'text': '<p>I used to work with PHP and recently I was asking myself, whats going on with this classmethod? Python manual is very technical and very short in words so it wont help with understanding that feature. I was googling and googling and I found answer -> <a href="http://code.anjanesh.net/2007/12/python-classmethods.html" rel="noreferrer">http://code.anjanesh.net/2007/12/python-classmethods.html</a>.</p>\n\n<p>If you are lazy to click it. My explanation is shorter and below. :)</p>\n\n<p>in PHP (maybe not all of you know PHP, but this language is so straight forward that everybody should understand what I\'m talking about) we have static variables like this:</p>\n\n<pre><code>\nclass A\n{\n\n    static protected $inner_var = null;\n\n    static public function echoInnerVar()\n    {\n        echo self::$inner_var."\\n";\n    }\n\n    static public function setInnerVar($v)\n    {\n        self::$inner_var = $v;\n    }\n\n}\n\nclass B extends A\n{\n}\n\nA::setInnerVar(10);\nB::setInnerVar(20);\n\nA::echoInnerVar();\nB::echoInnerVar();\n</code></pre>\n\n<p>The output will be in both cases 20.</p>\n\n<p>However in python we can add @classmethod decorator and thus it is possible to have output 10 and 20 respectively. Example:</p>\n\n<pre><code>\nclass A(object):\n    inner_var = 0\n\n    @classmethod\n    def setInnerVar(cls, value):\n        cls.inner_var = value\n\n    @classmethod\n    def echoInnerVar(cls):\n        print cls.inner_var\n\n\nclass B(A):\n    pass\n\n\nA.setInnerVar(10)\nB.setInnerVar(20)\n\nA.echoInnerVar()\nB.echoInnerVar()\n</code></pre>\n\n<p>Smart, ain\'t?</p>\n'}, {'answer_id': 2807576, 'author': 'Marvo', 'author_id': 337819, 'author_profile': 'https://Stackoverflow.com/users/337819', 'pm_score': 5, 'selected': False, 'text': '<p>I recently wanted a very light-weight logging class that would output varying amounts of output depending on the logging level that could be programmatically set.  But I didn\'t want to instantiate the class every time I wanted to output a debugging message or error or warning.  But I also wanted to encapsulate the functioning of this logging facility and make it reusable without the declaration of any globals.</p>\n\n<p>So I used class variables and the <code>@classmethod</code> decorator to achieve this.</p>\n\n<p>With my simple Logging class, I could do the following:</p>\n\n<pre><code>Logger._level = Logger.DEBUG\n</code></pre>\n\n<p>Then, in my code, if I wanted to spit out a bunch of debugging information, I simply had to code</p>\n\n<pre><code>Logger.debug( "this is some annoying message I only want to see while debugging" )\n</code></pre>\n\n<p>Errors could be out put with </p>\n\n<pre><code>Logger.error( "Wow, something really awful happened." )\n</code></pre>\n\n<p>In the "production" environment, I can specify </p>\n\n<pre><code>Logger._level = Logger.ERROR\n</code></pre>\n\n<p>and now, only the error message will be output.  The debug message will not be printed.</p>\n\n<p>Here\'s my class:</p>\n\n<pre><code>class Logger :\n    \'\'\' Handles logging of debugging and error messages. \'\'\'\n\n    DEBUG = 5\n    INFO  = 4\n    WARN  = 3\n    ERROR = 2\n    FATAL = 1\n    _level = DEBUG\n\n    def __init__( self ) :\n        Logger._level = Logger.DEBUG\n\n    @classmethod\n    def isLevel( cls, level ) :\n        return cls._level &gt;= level\n\n    @classmethod\n    def debug( cls, message ) :\n        if cls.isLevel( Logger.DEBUG ) :\n            print "DEBUG:  " + message\n\n    @classmethod\n    def info( cls, message ) :\n        if cls.isLevel( Logger.INFO ) :\n            print "INFO :  " + message\n\n    @classmethod\n    def warn( cls, message ) :\n        if cls.isLevel( Logger.WARN ) :\n            print "WARN :  " + message\n\n    @classmethod\n    def error( cls, message ) :\n        if cls.isLevel( Logger.ERROR ) :\n            print "ERROR:  " + message\n\n    @classmethod\n    def fatal( cls, message ) :\n        if cls.isLevel( Logger.FATAL ) :\n            print "FATAL:  " + message\n</code></pre>\n\n<p>And some code that tests it just a bit:</p>\n\n<pre><code>def logAll() :\n    Logger.debug( "This is a Debug message." )\n    Logger.info ( "This is a Info  message." )\n    Logger.warn ( "This is a Warn  message." )\n    Logger.error( "This is a Error message." )\n    Logger.fatal( "This is a Fatal message." )\n\nif __name__ == \'__main__\' :\n\n    print "Should see all DEBUG and higher"\n    Logger._level = Logger.DEBUG\n    logAll()\n\n    print "Should see all ERROR and higher"\n    Logger._level = Logger.ERROR\n    logAll()\n</code></pre>\n'}, {'answer_id': 3504391, 'author': 'Pierre', 'author_id': 140280, 'author_profile': 'https://Stackoverflow.com/users/140280', 'pm_score': 3, 'selected': False, 'text': '<p>Class methods provide a "semantic sugar" (don\'t know if this term is widely used) - or "semantic convenience".</p>\n\n<p>Example: you got a set of classes representing objects. You might want to have the class method <code>all()</code> or <code>find()</code> to write <code>User.all()</code> or <code>User.find(firstname=\'Guido\')</code>. That could be done using module level functions of course...</p>\n'}, {'answer_id': 3521920, 'author': 'Brandon Rhodes', 'author_id': 85360, 'author_profile': 'https://Stackoverflow.com/users/85360', 'pm_score': 6, 'selected': False, 'text': '<p>Think about it this way: normal methods are useful to hide the details of dispatch: you can type <code>myobj.foo()</code> without worrying about whether the <code>foo()</code> method is implemented by the <code>myobj</code> object\'s class or one of its parent classes. Class methods are exactly analogous to this, but with the class object instead: they let you call <code>MyClass.foo()</code> without having to worry about whether <code>foo()</code> is implemented specially by <code>MyClass</code> because it needed its own specialized version, or whether it is letting its parent class handle the call.</p>\n\n<p>Class methods are essential when you are doing set-up or computation that <em>precedes</em> the creation of an actual instance, because until the instance exists you obviously cannot use the instance as the dispatch point for your method calls. A good example can be viewed in the SQLAlchemy source code; take a look at the <code>dbapi()</code> class method at the following link:</p>\n\n<p><a href="https://github.com/zzzeek/sqlalchemy/blob/ab6946769742602e40fb9ed9dde5f642885d1906/lib/sqlalchemy/dialects/mssql/pymssql.py#L47" rel="noreferrer">https://github.com/zzzeek/sqlalchemy/blob/ab6946769742602e40fb9ed9dde5f642885d1906/lib/sqlalchemy/dialects/mssql/pymssql.py#L47</a></p>\n\n<p>You can see that the <code>dbapi()</code> method, which a database backend uses to import the vendor-specific database library it needs on-demand, is a class method because it needs to run <em>before</em> instances of a particular database connection start getting created — but that it cannot be a simple function or static function, because they want it to be able to call other, supporting methods that might similarly need to be written more specifically in subclasses than in their parent class. And if you dispatch to a function or static class, then you "forget" and lose the knowledge about which class is doing the initializing.</p>\n'}, {'answer_id': 6001200, 'author': 'firephil', 'author_id': 449946, 'author_profile': 'https://Stackoverflow.com/users/449946', 'pm_score': 3, 'selected': False, 'text': '<p>I think the most clear answer is <strong>AmanKow\'s</strong> one. It boils down to how u want to organize your code. You can write everything as module level functions which are wrapped in the namespace of the module i.e</p>\n\n<pre><code>module.py (file 1)\n---------\ndef f1() : pass\ndef f2() : pass\ndef f3() : pass\n\n\nusage.py (file 2)\n--------\nfrom module import *\nf1()\nf2()\nf3()\ndef f4():pass \ndef f5():pass\n\nusage1.py (file 3)\n-------------------\nfrom usage import f4,f5\nf4()\nf5()\n</code></pre>\n\n<p>The above procedural code is not well organized, as you can see after only 3 modules it gets confusing, what is each method do ? You can use long descriptive names for functions(like in java) but still  your code gets unmanageable very quick.</p>\n\n<p>The object oriented way is to break down your code into manageable blocks i.e Classes &amp; objects and functions can be associated with objects instances or with classes. </p>\n\n<p>With class functions you gain another level of division in your code compared with module level functions.\nSo you can group related functions within a class to make them more specific to a task  that you assigned to that class. For example you can create a file utility class :</p>\n\n<pre><code>class FileUtil ():\n  def copy(source,dest):pass\n  def move(source,dest):pass\n  def copyDir(source,dest):pass\n  def moveDir(source,dest):pass\n\n//usage\nFileUtil.copy("1.txt","2.txt")\nFileUtil.moveDir("dir1","dir2")\n</code></pre>\n\n<p>This way is more flexible and more maintainable, you group functions together and its more obvious to what each function do. Also you prevent name conflicts, for example the function copy may exist in another imported module(for example network copy) that you use in your code, so when you use the full name FileUtil.copy() you remove the problem and both copy functions can be used side by side.</p>\n'}, {'answer_id': 9867663, 'author': 'Rusty Rob', 'author_id': 632088, 'author_profile': 'https://Stackoverflow.com/users/632088', 'pm_score': 3, 'selected': False, 'text': '<p>When a user logs in on my website, a User() object is instantiated from the username and password.</p>\n\n<p>If I need a user object without the user being there to log in (e.g. an admin user might want to delete another users account, so i need to instantiate that user and call its delete method):</p>\n\n<p>I have class methods to grab the user object.</p>\n\n<pre><code>class User():\n    #lots of code\n    #...\n    # more code\n\n    @classmethod\n    def get_by_username(cls, username):\n        return cls.query(cls.username == username).get()\n\n    @classmethod\n    def get_by_auth_id(cls, auth_id):\n        return cls.query(cls.auth_id == auth_id).get()\n</code></pre>\n'}, {'answer_id': 12312026, 'author': 'Peter Moore', 'author_id': 1223946, 'author_profile': 'https://Stackoverflow.com/users/1223946', 'pm_score': 2, 'selected': False, 'text': '<p>This is an interesting topic. My take on it is that python classmethod operates like a singleton rather than a factory (which returns a produced an instance of a class). The reason it is a singleton is that there is a common object that is produced (the dictionary) but only once for the class but shared by all instances.</p>\n\n<p>To illustrate this here is an example. Note that all instances have a reference to the single dictionary. This is not Factory pattern as I understand it. This is probably very unique to python.</p>\n\n<pre><code>class M():\n @classmethod\n def m(cls, arg):\n     print "arg was",  getattr(cls, "arg" , None),\n     cls.arg = arg\n     print "arg is" , cls.arg\n\n M.m(1)   # prints arg was None arg is 1\n M.m(2)   # prints arg was 1 arg is 2\n m1 = M()\n m2 = M() \n m1.m(3)  # prints arg was 2 arg is 3  \n m2.m(4)  # prints arg was 3 arg is 4 &lt;&lt; this breaks the factory pattern theory.\n M.m(5)   # prints arg was 4 arg is 5\n</code></pre>\n'}, {'answer_id': 14042417, 'author': 'yet', 'author_id': 373451, 'author_profile': 'https://Stackoverflow.com/users/373451', 'pm_score': 4, 'selected': False, 'text': '<p>It allows you to write generic class methods that you can use with any compatible class.</p>\n\n<p>For example:</p>\n\n<pre class="lang-python prettyprint-override"><code>@classmethod\ndef get_name(cls):\n    print cls.name\n\nclass C:\n    name = "tester"\n\nC.get_name = get_name\n\n#call it:\nC.get_name()\n</code></pre>\n\n<p>If you don\'t use <code>@classmethod</code> you can do it with self keyword but it needs an instance of Class:</p>\n\n<pre class="lang-python prettyprint-override"><code>def get_name(self):\n    print self.name\n\nclass C:\n    name = "tester"\n\nC.get_name = get_name\n\n#call it:\nC().get_name() #&lt;-note the its an instance of class C\n</code></pre>\n'}, {'answer_id': 26354620, 'author': "Dee'Kej", 'author_id': 3481531, 'author_profile': 'https://Stackoverflow.com/users/3481531', 'pm_score': 2, 'selected': False, 'text': '<p>I was asking myself the same question few times. And even though the guys here tried hard to explain it, IMHO the best answer (and simplest) answer I have found is the <a href="https://docs.python.org/3/library/functions.html?highlight=classmethod#classmethod" rel="nofollow">description</a> of the Class method in the Python Documentation.</p>\n\n<p>There is also reference to the Static method. And in case someone already know instance methods (which I assume), this answer might be the final piece to put it all together...</p>\n\n<p>Further and deeper elaboration on this topic can be found also in the documentation:\n<a href="https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy" rel="nofollow">The standard type hierarchy</a> (scroll down to <em>Instance methods</em> section)</p>\n'}, {'answer_id': 39381275, 'author': 'Ben Hyde', 'author_id': 2101337, 'author_profile': 'https://Stackoverflow.com/users/2101337', 'pm_score': 0, 'selected': False, 'text': "<p>A class defines a set of instances, of course.  And the methods of a class work on the individual instances.  The class methods (and variables) a place to hang other information that is related to the set of instances over all.</p>\n\n<p>For example if your class defines a the set of students you might want class variables or methods which define things like the set of grade the students can be members of.</p>\n\n<p>You can also use class methods to define tools for working on the entire set.  For example Student.all_of_em() might return all the known students.  Obviously if your set of instances have more structure than just a set you can provide class methods to know about that structure.  Students.all_of_em(grade='juniors')</p>\n\n<p>Techniques like this tend to lead to storing members of the set of instances into data structures that are rooted in class variables.  You need to take care to avoid frustrating the garbage collection then.</p>\n"}, {'answer_id': 43008840, 'author': 'starfry', 'author_id': 712506, 'author_profile': 'https://Stackoverflow.com/users/712506', 'pm_score': 2, 'selected': False, 'text': '<p>What just hit me, coming from Ruby, is that a so-called <em>class</em> method and a so-called <em>instance</em> method is just a function with semantic meaning applied to its first parameter, which is silently passed when the <em>function</em> is called as a method of an <em>object</em> (i.e. <code>obj.meth()</code>).</p>\n\n<p>Normally that <em>object</em> must be an instance but the <code>@classmethod</code> <a href="https://www.python.org/dev/peps/pep-0318" rel="nofollow noreferrer">method decorator</a> changes the rules to pass a class. You can call a class method on an instance (it\'s just a function) - the first argument will be its class. </p>\n\n<p>Because it\'s just a <em>function</em>, it can only be declared once in any given scope (i.e. <code>class</code> definition). If follows therefore, as a surprise to a Rubyist, that <strong>you can\'t have a class method and an instance method with the same name</strong>.</p>\n\n<p>Consider this:</p>\n\n<pre><code>class Foo():\n  def foo(x):\n    print(x)\n</code></pre>\n\n<p>You can call <code>foo</code> on an instance </p>\n\n<pre><code>Foo().foo()\n&lt;__main__.Foo instance at 0x7f4dd3e3bc20&gt;\n</code></pre>\n\n<p>But not on a class:</p>\n\n<pre><code>Foo.foo()\nTraceback (most recent call last):\n  File "&lt;stdin&gt;", line 1, in &lt;module&gt;\nTypeError: unbound method foo() must be called with Foo instance as first argument (got nothing instead)\n</code></pre>\n\n<p>Now add <code>@classmethod</code>:</p>\n\n<pre><code>class Foo():\n  @classmethod\n  def foo(x):\n    print(x)\n</code></pre>\n\n<p>Calling on an instance now passes its class:</p>\n\n<pre><code>Foo().foo()\n__main__.Foo\n</code></pre>\n\n<p>as does calling on a class:</p>\n\n<pre><code>Foo.foo()\n__main__.Foo\n</code></pre>\n\n<p><em>It\'s only convention that dictates that we use <code>self</code> for that first argument on an instance method and <code>cls</code> on a class method. I used neither here to illustrate that it\'s just an argument. In Ruby, <code>self</code> is a</em> keyword.</p>\n\n<p>Contrast with Ruby:</p>\n\n<pre><code>class Foo\n  def foo()\n    puts "instance method #{self}"\n  end\n  def self.foo()\n    puts "class method #{self}"\n  end\nend\n\nFoo.foo()\nclass method Foo\n\nFoo.new.foo()\ninstance method #&lt;Foo:0x000000020fe018&gt;\n</code></pre>\n\n<p>The Python class method is just a <em>decorated function</em> and you can use the same techniques to <a href="http://thecodeship.com/patterns/guide-to-python-function-decorators" rel="nofollow noreferrer">create your own decorators</a>. A decorated method wraps the real method (in the case of <code>@classmethod</code> it passes the additional class argument). The underlying method is still there, hidden <a href="https://github.com/mapleoin/undecorated" rel="nofollow noreferrer">but still accessible</a>.</p>\n\n<hr>\n\n<p><em>footnote: I wrote this after a name clash between a class and instance method piqued my curiosity. I am far from a Python expert and would like comments if any of this is wrong.</em></p>\n'}, {'answer_id': 58878894, 'author': 'psilocybin', 'author_id': 4147666, 'author_profile': 'https://Stackoverflow.com/users/4147666', 'pm_score': 1, 'selected': False, 'text': "<p><code>@classmethod</code> can be useful for easily instantiating objects of that class from outside resources. Consider the following:</p>\n\n<pre><code>import settings\n\nclass SomeClass:\n    @classmethod\n    def from_settings(cls):\n        return cls(settings=settings)\n\n    def __init__(self, settings=None):\n        if settings is not None:\n            self.x = settings['x']\n            self.y = settings['y']\n</code></pre>\n\n<p>Then in another file:</p>\n\n<pre><code>from some_package import SomeClass\n\ninst = SomeClass.from_settings()\n</code></pre>\n\n<p>Accessing inst.x will give the same value as settings['x'].</p>\n"}, {'answer_id': 63481357, 'author': 'Sanmitha Sadhishkumar', 'author_id': 13827419, 'author_profile': 'https://Stackoverflow.com/users/13827419', 'pm_score': 0, 'selected': False, 'text': "<p>Classes and Objects concepts are very useful in organizing things. It's true that all the operations that can be done by a method can also be done using a static function.</p>\n<p>Just think of a scenario, to build a Students Databases System to maintain student details.\nYou need to have details about students, teachers and staff. You need to build functions to calculate fees, salary, marks, etc. Fees and marks are only applicable for students, salary is only applicable for staff and teachers. So if you create separate classes for every type of people, the code will be organized.</p>\n"}, {'answer_id': 64445340, 'author': 'sebtac', 'author_id': 8227979, 'author_profile': 'https://Stackoverflow.com/users/8227979', 'pm_score': 3, 'selected': False, 'text': '<p>if you are not a &quot;programmer by training&quot;, this should help:</p>\n<p>I think I have understood the technical explanations above and elsewhere on the net, but I was always left with a question &quot;Nice, but why do I need it? What is a practical, use case?&quot;. and now life gave me a good example that clarified all:</p>\n<p>I am using it to control the global-shared variable that is shared among instances of a class instantiated by multi-threading module. in humane language, I am running multiple agents that create examples for deep learning IN PARALLEL. (imagine multiple players playing ATARI game at the same time and each saving the results of their game to one common repository (the SHARED VARIABLE))</p>\n<p>I instantiate the players/agents with the following code (in Main/Execution Code):</p>\n<pre class="lang-py prettyprint-override"><code>a3c_workers = [A3C_Worker(self.master_model, self.optimizer, i, self.env_name, self.model_dir) for i in range(multiprocessing.cpu_count())]\n</code></pre>\n<ul>\n<li>it creates as many players as there are processor cores on my comp\nA3C_Worker - is a class that defines the agent\na3c_workers - is a list of the instances of that class (i.e. each instance is one player/agent)</li>\n</ul>\n<p>now i want to know how many games have been played across all players/agents thus within the A3C_Worker definition I define the variable to be shared across all instances:</p>\n<pre class="lang-py prettyprint-override"><code>class A3C_Worker(threading.Thread):\n   global_shared_total_episodes_across_all_workers = 0\n</code></pre>\n<p>now as the workers finish their games they increase that count by 1 each for each game finished</p>\n<p>at the end of my example generation i was closing the instances but the shared variable had assigned the total number of games played. so when I was re-running it again my initial total number of episodes was that of the previous total. but i needed that count to represent that value for each run individually</p>\n<p>to fix that i specified :</p>\n<pre class="lang-py prettyprint-override"><code>class A3C_Worker(threading.Thread):\n    @classmethod\n    def reset(cls):\n        A3C_Worker.global_shared_total_episodes_across_all_workers = 0\n</code></pre>\n<p>than in the execution code i just call:</p>\n<pre class="lang-py prettyprint-override"><code>A3C_Worker.reset()\n</code></pre>\n<p>note that it is a call to the CLASS overall not any INSTANCE of it individually. thus it will set my counter to 0 for every new agent I initiate from now on.</p>\n<p>using the usual method definition <code>def play(self):</code>, would require us to reset that counter for each instance individually, which would be more computationally demanding and difficult to track.</p>\n'}]
['https://Stackoverflow.com/questions/38238', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3171/']