I come from Ruby and I'm getting used to the "explicit over implicit" philosophy that Python follows, but I was confused earlier about how I actually make class methods. Now, I want verification that I am indeed correct in the following:
Every method in a class is essentially a class method in Python.
For example:
class Employee(object):
employeeCount = 0
def __init__(self, name):
self.name = name
employeeCount += 1
def get_name(self):
return self.name
def get_employee_count():
return employeeCount
If I understand this correct, for instance david = Employee("david") the following two are equivalent:
david.get_name() and Employee.get_name(david). Likewise, it makes sense that we could say get_employee_count is a class method too, but it doesn't rely on any instance, hence why we don't pass in an instance. This is why it doesn't make sense to type david.get_employee_count(), because this would be Employee.get_employee_count(david), but get_employee_count doesn't take in a parameter, namely the instance. This concludes that we would just type Employee.get_employee_count().
Am I correct in my thinking? Thank you.
get_employee_count()is a not a class method, it's a normal function inside a class.(or static method in py2.x, if used with @staticmethod)e = Employee("David"); print e.get_employee_count()won't work becauseget_employee_countdoesn't know whereemployeeCountisselfparameter, so that'll raise an exception.Employee.get_employee_count().