[PYTHON] Class methods and static methods

class Person(object):

    kind = 'human'

    def __init__(self):
        self.x = 100

    #Methods that can be called without creating an object
    @classmethod
    def what_is_your_kind(cls):
        return cls.kind
    
    #Same as a function outside the class
    @staticmethod
    def about(year):
        print('about human {}'.format(year))

a = Person()
print(a.what_is_your_kind())

#Can be called without creating an object
print(Person.what_is_your_kind())#human
print(Person.about(2020))#about human 2020

Recommended Posts

Class methods and static methods
Various class methods and static methods
Class method static method
Example of using class variables and class methods
[Python] Difference between class method and static method
About Class and Instance
Python class variables and instance variables
Class inheritance and super functions
I'll inherit and override the class
Combinatorial optimization-typical problems and execution methods
Use instance method and class method properly
perl objects and python class part 2.
Python class definitions and instance handling
class
(For myself) Flask_ex (templates and static)
perl objects and python class part 1.
class
Python classes and instances, instance methods
Bind methods to Python classes and instances
Bind to class to read and write YAML
Python basic operation 3rd: Object-oriented and class
Comparison of class inheritance and constructor description
Private methods and fields in python [encryption]
Talking about Python class attributes and metaclasses