Example of using class variables and class methods

hoge.py


class hoge(object):
    ham = 0 

    def __init__(self):
        self.spam = 0

    def print(self):
        print('class.ham ' + str(hoge.ham))
        print('self.ham '+ str(self.ham) )
        print('self.spam ' + str(self.spam))

        self.clsM()#Class method is self.Can be called from

    def increment(self):
        self.spam += 1
        self.ham += 1 #Image that is incremented after being assigned to the class variable ham → instance variable ham

    @classmethod
    def clsM(cls):
        print('called class method')

a = hoge()
a.increment()
a.print()

Execution result

python


class.ham 5
self.ham 6
self.spam 1
called class method

Recommended Posts

Example of using class variables and class methods
Class methods and static methods
Example of using lambda
Checking methods and variables using the library see
Various class methods and static methods
Python: Class and instance variables
Globalization of class instance variables
Python class variables and instance variables
Reference order of class variables and instance variables in "self. Class variables" in Python
Benefits and examples of using RabbitMq
Calculation of homebrew class and existing class
Class variables
Class variables
Execution example of blob detection using OpenCV
Comparison of class inheritance and constructor description
Correlation visualization of features and objective variables
List of frequently used built-in functions and methods
Organize the meaning of methods, classes and objects
[python] Difference between variables and self. Variables in class
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
Trouble with Python pseudo-private variables and class inheritance
Smoothing of time series and waveform data 3 methods (smoothing)
Frequently used methods of Selenium and Beautiful Soup
[Python] Class type and usage of datetime module
Summary of pickle and unpickle processing of user-defined class
Example of reading and writing CSV with Python
Causal reasoning using machine learning (organization of causal reasoning methods)
Difference between Ruby and Python in terms of variables
Evaluate and concatenate strings in variables using eval in bash
Sample of getting module name and class name in Python
Collection and automation of erotic images using deep learning
Overview of class-based generic views and inherited class relationships
[Python] Types of statistical values (features) and calculation methods
About Class and Instance
Global and local variables 2
parallelization of class method
[python] class basic methods
Implementation example of centrally managing inventory and variables by linking Ansible and serverspec (supports multiple servers)
Global and local variables 1
Sample program and execution example of ensemble learning (Stacked generalization)
Calculation of odometry using CNN and depth estimation Part 2 (CNN SLAM # 2)
Examination of Forecasting Method Using Deep Learning and Wavelet Transform-Part 2-
Get data using Ministry of Internal Affairs and Communications API
[Python] How to play with class variables with decorator and metaclass
[Machine learning] Feature selection of categorical variables using chi-square test
Using MLflow with Databricks ② --Visualization of experimental parameters and metrics -
python> Example of using strip ()> src = '0123456789ABCDEF' / wrk = src.strip ('DEF')
Verification and implementation of video reconstruction method using GRU and Autoencoder
Derivation of EM algorithm and calculation example for coin toss
Reconstruction of cone beam CT (CBCT) using python and TIGRE
Explanation of CSV and implementation example in each programming language
Apply monkey patch to all methods of pytest test class