Python static class objects

Static Class in Python

Static Class in Python

  1. Use the @staticmethod Decorator to Create a Static Class in Python
  2. Use the @classmethod Decorator to Create a Static Class in Python
  3. Use a Module File to Create a Static Class in Python

A static class is a handy feature in programming. A static class cannot be inherited and cannot be instantiated. There is no direct way to make a class static. In Python, we can implement a static class by making its methods and variables static.

In this article, we will implement such methods.

Use the @staticmethod Decorator to Create a Static Class in Python

To implement a static class, we can make its methods and variables static. For this, we can use the @staticmethod decorator to create methods in a class static. A decorator is a special function specified before a function and takes the whole function as a parameter.

class abc(object):  @staticmethod  def static_method():  print("Hi,this tutorial is about static class in python")  abc.static_method() 
Hi,this tutorial is about static class in python 

Note that this method only works on versions of Python higher than 2.6.

Читайте также:  Python gui mac os

Use the @classmethod Decorator to Create a Static Class in Python

The @classmethod can make a method static to the class and not its object. It has several advantages over the @staticmethod decorator. It will also work with subclasses and can modify the class state.

However, it requires a compulsory argument to be included in the method called cls .

class abc(object):  @classmethod  def static_method(cls):  print("Hi,this tutorial is about static class in python")  abc.static_method() 
Hi,this tutorial is about static class in python 

Use a Module File to Create a Static Class in Python

In Python, probably the best way to implement a static class is by creating a module. We can import a module, and its functions can be accessed using the module name. It does not require any objects to be instantiated, and it cannot be inherited.

See the following example.

def static_method():  print("Hi this is the content of the module file")  

The above code can be a simple function in a Python script that can be imported as a module.

For example, if the name of the above Python script is abc.py , we can do something as shown below.

import abc abc.static_method() 
Hi,this tutorial is about static class in python 

Related Article — Python Class

Copyright © 2023. All right reserved

Источник

How do I create static class data and static class methods in Python?

Python includes the concept of static class data and static class methods.

The static class data

Here, define a class attribute for the static class data. Explicitly use the class name in the assignment, if you want to assign a new value to the attribute −

class Demo: count = 0 def __init__(self): Demo.count = Demo.count + 1 def getcount(self): return Demo.count

We can also return the following instead return Demo.count −

Within a method of Demo, an assignment like self.count = 42 creates a new and unrelated instance named count in self’s own dict. Rebinding of a class-static data name must always specify the class whether inside a method or not −

The static class methods

Let us see how static methods work. A static method is bound to the class and not the object of the class. The statis methods are used to create utility functions.

Static Method cannot access or modify the class state. The Static methods do not know about class state. These methods are used to do some utility tasks by taking some parameters.

Remember, the @staticmethod decorator is used to create a static method as show below −

class Demo: @staticmethod def static(arg1, arg2, arg3): # No 'self' parameter! ...

Example

Let us 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 token punctuation">,st1.age) print("Student2 Age token punctuation">,st2.age) print("Student3 Age token punctuation">,st3.age) print("Student4 Age token punctuation">,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

Источник

Static Class Variables and Methods in Python

Be on the Right Side of Change

Static class variables and methods in Python:

  • How do I create static class variables or methods in Python?
  • Are static class variables possible in Python?

This article will give you a comprehensive answer to these questions!

Static Class Variables

Next, we’ll explore the code in this graphic in 4 easy steps.

Step 1: Class Attribute in Python

If we try to summarize what “Static class variable” means in other programming languages, we can say that:

💡 Definition: A static class variable is a variable shared by all the instances of a class and there is only one copy of this variable and not a copy in each instance of the class.

In Python, it is common to refer to this concept as “class attribute” but also as “attribute class variable”, “class variable” and even, although less common, as “static variable” or “static class variable”. Here, to make the terminology simple, we adopt the name “class attribute”.

Let’s see how to create and work with “class attributes” in Python.

In the following example, we will model the employees of a company named «A» with a class called Employees .

class Employees: company = "A" # class attribute languages = []

We point out that each instance of the class (each concrete employee) belongs to company «A» with a class attribute company that has the value «A» .

Then we create another “class attribute” called language as an empty list to store later the native language of each employee.

As you are probably thinking this should be an instance attribute. But it is here to help you see that mutable types used as class attributes can give surprising results. We will see this later.

Step 2: Instance Attribute in Python

As you can see it can be created inside the class definition, as you create any variable in Python, but outside the methods starting with def .

Now we are going to define with __init__() the property “ n ” (from name) with which each object (instance) of the class Employees must be initialized.

These properties are called “data attributes” or “instance attributes” and correspond to data members in C++.

class Employees: company = "A" # class atribute languages = [] def __init__(self, name): self.n = name # data or instance attribute

Step 3: Creating Instances and Print Class Attribute in Python

Well now we are going to add code to create two instances and print their class attribute:

class Employees: company = "A" # class atribute languages = [] def __init__(self, name): self.n = name # data or instance atribute e1 = Employees("Peter White") e2 = Employees("Paul Green") print(e1.company) # Result: A print(e2.company) # Result: A

Step 4: Modifying a Class Attribute

In Python, we can change the value of a “class attribute” in the code and the changes will be reflected in all instances as we show in the code below:

# This code shows only what is added to the previous code and the results you will see Employees.company = "B" # Changing the value of the class attribute print(e1.company) # Result: B print(e2.company) # Result: B

Step 5: Modifying a Class Attribute to Become an Instance Attribute

And in Python, we can change the “class attribute” to an instance. It could be rare but possible, as we show in the next example with the instance called “ e1 “:

# This code shows only what is added to the previous code and the results you will see e1.company = "C" # Changing the class attribute of the instance e1 print(e1.company) # Result: C print(e2.company) # Result: B

Step 6: Understanding Mutable Class Attributes

To finish we should not use as “class attribute” mutable data types like lists or dictionaries because they could have surprising behaviors if we use them as in the example below.

Here we try to put the native languages of each employee as a class attribute and we start it as an empty list.

Clearly, this is an “instance attribute” but let’s force the example a little to see what happens:

# This code shows only what is added to the previous code and the results you will see e1.language.append("english") e2.language.append("spanish") print(e1.language) # Result: ['english', 'spanish'] print(e2.language) # Result: ['english', 'spanish']

As can be seen, the expected results were not obtained.

  • On the one hand, this helps to understand that the native language is a property of each employee, that is, of each instance, and should be added as “ self.n ” is inside the __init__() method.
  • On the other hand, it helps to see, as stated in point 9.3.5 of the documentation, that implementing mutable data types as “class attributes” can have possibly surprising effects.

To conclude, I suggest you look at the figure at the beginning of this section where we have tried to summarize all these considerations.

Static Methods

As it is said in the documentation, static methods in Python are similar to those found in Java or C++:

A static method is a method that can run without any instance of the class. It doesn’t need to be invoked for a particular object.

If you want to quickly understand how to create and work with a static method I suggest you look at the following figure that summarizes the most important.

Understanding static method Python example

The most modern Python implementation, the one we show here, requires the @staticmethod decorator before the def that starts the method.

This tells Python that this is a “static method” and not any other.

As a “static method” does not modify or need a class instance to execute it does not require an argument such as “ self ” which represents the instance itself from which a method is called

💡 Note: We clarify that using the term “ self ” is a convention and another word could be used but this could make the code less readable because everyone understands and expects to see a “ self ” in any instance method as the first argument.

Let’s see an example in which we retake the Employees class built in the previous section. Trying to show something simple we are going to invent a first static method that returns the version of the program when it is called.

As it is seen it is not necessary to create an instance to call it.

class Employees: company = "A" def __init__(self,name): self.n = name @staticmethod def version(): print("""Welcome to Company A's employee system. This is the version 1.0 Type "Employees.help(1)" to see how to enter an object.""") # To call this static method: Employees.version() # Result: Welcome to Company…

In the previous case the “static method” had no argument.

Let’s look at another one that needs, as the first “static method” message says, a 1 to display a first help message to create an object or instance of the class.

# This code shows only what is added to the previous code and the results you will see @staticmethod def help(key): if key == 1: print("To create an object you can follow the next example by putting the employee's complete name in parentheses: e1 = Employees('John Black')") # To call this static method: Employees.help(1) # Show: To create an object…

Conclusions

We have seen how to implement “static class variables” which in Python are more commonly called “class attributes”.

They are simply created like any variable but inside the class definition and outside the “ def ” that creates methods.

We’ve also seen some peculiarities that class attributes have in Python.

On the other hand, we have seen how to implement “static methods”, which do not require any instance to be used, using the “ @staticmethod ” decorator before the “ def ” with which we start the definition of the method itself.

We also saw that it does not require “ self ” as the first argument.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Оцените статью