0% found this document useful (0 votes)
3 views3 pages

Understanding Python Private and Static Methods

Uploaded by

jscomputer700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Understanding Python Private and Static Methods

Uploaded by

jscomputer700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Private Access Modifier

Class properties and methods with private access modifier can only be accessed within the class where they are
defined and cannot be accessed outside the class. In Python private properties and methods are declared by adding a
prefix with two underscores(‘__’) before their declaration.

Built-In Class Attributes in Python


The built-in class attributes provide us with information about the class.

Using the dot (.) operator, we may access the built-in class attributes.

What is Python Static Method?


In Python, a static method is a type of method that does not require any instance to be called. It is
very similar to the class method but the difference is that the static method doesn't have a
mandatory argument like reference to the object − self or reference to the class − cls.

Static methods are used to access static fields of a given class. They cannot modify the state of a class
since they are bound to the class, not instance.

How to Create Static Method in Python?

There are two ways to create Python static methods −

 Using staticmethod() Function

 Using @staticmethod Decorator

 Using staticmethod() Function


 Python's standard library function named staticmethod() is used to create a static method. It accepts a
method as an argument and converts it into a static method.

 Syntax
 staticmethod(method)

 Example
Using @staticmethod Decorator

The second way to create a static method is by using the Python @staticmethod decorator. When we
use this decorator with a method it indicates to the Interpreter that the specified method is static.

Syntax

You might also like