


How to create static class data and static class methods in Python?
Python includes the concepts of static class data and static class methods.
Static class data
Here, define a class attribute for static class data. If you want to assign a new value to a property, explicitly use the class name -
in the assignmentclass Demo: count = 0 def __init__(self): Demo.count = Demo.count + 1 def getcount(self): return Demo.count
Instead of returning Demo.count -
we can also return the followingreturn self.count
In Demo's method, an assignment like self.count = 42 creates a new, unrelated instance named count in self's own dictionary. Rebinding of class static data names must always specify the class, whether inside a method or not -
Demo.count = 314
Static class method
Let's see how static methods work. Static methods are bound to a class rather than an object of the class. The status method is used to create utility functions.
Static methods cannot access or modify class state. Static methods have no knowledge of class state. These methods are used to perform some practical tasks by getting some parameters.
Remember, the @staticmethod decorator is used to create static methods as shown below -
class Demo: @staticmethod def static(arg1, arg2, arg3): # No 'self' parameter! ...
Example
Let’s see a complete example -
from datetime import date class Student: def __init__(self, name, age): self.name = name self.age = age # A class method @classmethod def birthYear(cls, name, year): return cls(name, date.today().year - year) # A static method # If a Student is over 18 or not @staticmethod def checkAdult(age): return age > 18 # Creating 4 objects st1 = Student('Jacob', 20) st2 = Student('John', 21) st3 = Student.birthYear('Tom', 2000) st4 = Student.birthYear('Anthony', 2003) print("Student1 Age = ",st1.age) print("Student2 Age = ",st2.age) print("Student3 Age = ",st3.age) print("Student4 Age = ",st4.age) # Display the result print(Student.checkAdult(22)) print(Student.checkAdult(20))
Output
Student1 Age = 20 Student2 Age = 21 Student3 Age = 22 Student4 Age = 19 True True
The above is the detailed content of How to create static class data and static class methods in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
