Staticmethod, Classmethod and Property

It’s not so commonly used in data science, but as an OOP language, Python can make good use of class, object. Unique to Python, there are staticmethod, classmethod, property written by professional programmers.

In short, a staticmethod is a method that knows nothing about the class or instance it was called on, while a classmethod provides a way to creation a function like constructor __init__. For example, in the below class of person, classmethod allows to create a person object by calling Person.fromBirthYear(parameters), while staticmethod is independent without feeding any self settings.

from datetime import date 
class Person: 
    def __init__(self, name, age): = name 
        self.age = age 
    # a class method to create a Person object by birth year. 
    def fromBirthYear(cls, name, year): 
        return cls(name, - year) 
    # a static method to check if a Person is adult or not. 
    def isAdult(age): 
        return age > 18
person1 = Person('mayank', 21) 
person2 = Person.fromBirthYear('mayank', 1996) 
print person1.age 
print person2.age 

Another example here:

class RomanNumeral:
    SYMBOLS = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5}
    def __init__(self, number):
        self.value = number
    def from_string(cls, string):
        return cls(roman_to_int(string))
    def roman_to_int(numeral):
        total = 0
        for symbol, next_symbol in zip_longest(numeral, numeral[1:]):
            value = RomanNumeral.SYMBOLS[symbol]
            next_value = RomanNumeral.SYMBOLS.get(next_symbol, 0)
            if value < next_value:
                value = -value
            total += value
        return total    

On the other hand, there is a problem in class object that it is needed to change the setting of certain attributes arbitrarily. One way of doing it is to use @property and then setter. For example, below __population is particularly set to be private, and then population attribute can be changed.

class Golem:
    __population = 0
    __life_span = 10
    def __init__(self, name=None): = name
        self.built_year =
        self.__active = True
        Golem.__population += 1
    def say_hi(self):
    def cease(self):
        self.__active = False
        Golem.__population -= 1
    def is_active(self):
        if - self.built_year >= Golem.__life_span:
        return self.__active
    def population(self):
        return Golem.__population

    def population(self, value):
        Golem.__population = value
g = Golem('Clay')

to quickly set the attr use:

setattr(Golem, ‘population’, 100)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.