Object and Static methods - what is the difference; example in Python 3
Archive - Originally posted on "The Horse's Mouth" - 2016-02-17 23:46:41 - Graham EllisThere are two types of methods (named pieces of code) that you can use in a class.
Object methds - occasionally called dynamic methods - operate on an object within a class ("one particular thing of a type") and will always have some way of referencing the elements (variables) within an object of the class. Depending on the language you're using, that may be the keyword self or this, or it may be the first positional parameter.
You need to set up an object before you can call object methods (on it) - that's done with a special method known as a constructor. In many languages, if you don't define you rown constructor, there's a default one for the system to fall back on. And if you deo define your own constructor (if you want to pass in initial settings, for example) it may have its own special reserved name (e.g. Python), or it may have the same name as in the class (e.g. Java)
Here's an example of the definition of a class, with object methods, and their use in Python 3:
class square:
def __init__(self,dimension,city):
self.size = dimension
self.city = city
def getArea(self):
return self.size * self.size
def getEdge(self):
return 4 * self.size
def getCountry(self):
return (self.city.split(", ")[1])
trafalgar = square(110,"London, UK")
plazaDeBalcarce = square(300,"Balcarce, Argentina")
print ("circumference of {}m in {}".format(
trafalgar.getEdge(), trafalgar.getCountry()))
print ("circumference of {}m in {}".format(
plazaDeBalcarce.getEdge(), plazaDeBalcarce.getCountry()))
Object methods will usually account for the majority of methods you define and call in a class. However, there will be times that you want to associate a utility method with a class, most commonly if you want to perform a piece of logic that relates to your data type before you've actually created any objects.Such utility methods are known as class methods or static methods and they are called 'on' the class as a whole, rather than on an individual object.
Here's an example developed from the one above where I've used a static or class method to perform a piece of logic that's inherently associated with objects of type "square", but which I potentially needed to perform before I had any objects of the type actually created - hence the need for a static.
class square:
def __init__(self,dimension,city):
self.size = dimension
self.city = city
def getArea(self):
return self.size * self.size
def getEdge(self):
return 4 * self.size
def getCountry(self):
return (self.city.split(", ")[1])
def placeValidate(rawtest):
result = len(rawtest.split(", ")) == 2
return result
edge = 77
locate = input("Where is the square [city, country]? ")
if square.placeValidate(locate):
projected = square(edge,locate)
print ("circumference of {}m in {}".format(
projected.getEdge(), projected.getCountry()))
else:
print ("Sorry - I don't understand")
These examples are in Python 3. In other languages, you may need to declare the static method to be "static" or use a decorator such as @staticmethod or @classmethod.
In most languages, as well as calling a static method on the class, you can call it on an already existing object of the class ... you won't have a access to the elements within the object in the method if you do this, but your program will understand which particular method it is that it has to run, as their could be lots of utiities all of the same name in your code over all.
The most common use of static methods on our training courses is in what are called factory methods, where a piece of raw data - typically a text string - that has all the definitions needed to create an object is read in, and there's code needed to analyse that text string and actually call the apporpriate constructor. Such code is really utility code, and it belongs (for sharing and mainenance purposes) with the class and not with the calling code.
Sample code (first example) [here] and (second example) [here]. Both examples include sample outputs. Topic covered on our Python Programming courses.