Python class member scope summary

Python in Advent Calendar is depopulated, so I'll continue to write some simple material yesterday.

Here is a summary of the scopes of Python class members summarized earlier.

Specifically, how to write and access public members and methods, and how to write and access private members and methods.

# coding: utf-8

class Widget(object):

    #constructor
    def __init__(self, r, l):

        #Normal member variable
        self.rval = r
        self.lval = l

        #Private variables
        self.__secret = 5

    #public class member variable
    classVal = 30

    #Private class variables
    #It can only be accessed from the outside with a special description
    __SecretClassVal = 10

    #Normal method
    def Calc(self):
        #Member variables can be defined here as well.
        self.top = 10
        return self.rval * self.lval * self.top

    #Private method
    def __MyCalc(self):
        print "This is Private Method!"

    #Class method.
    @classmethod
    def SelfName(cls):
        #Class member variables can be defined here as well.
        cls.number = 1

    #Private class method.
    @classmethod
    def __PrivateSelfName(cls):
        print "This is Private Class Method!"

if __name__ == '__main__':

    #Calls to constructors and regular methods.
    w = Widget(2, 4)

    #Access to member variables
    w.lval = 3
    w.rval = 4

    #Access private member variables.
    #instance._name of the class__It can be accessed by variable name.(Not recommended)
    print w._Widget__secret

    #Access public class variables.
    #You can access either the instance name or the class name.
    print Widget.classVal
    print w.classVal

    #Access private class variables.
    #instance._name of the class__It can be accessed by variable name.(Not recommended)
    print w._Widget__SecretClassVal

    #Normal method call.
    print w.Calc()

    #Calling a private method(Not recommended)
    print w._Widget__MyCalc()

    #Calling a class method.
    Widget.SelfName()

    #Calling a private class method(Not recommended)
    print w._Widget__PrivateSelfName()

output


 5
 30
 30
 10
 120
 This is Private Method!
 None
 This is Private Class Method!
 None

Recommended Posts

Python class member scope summary
Python Summary
Python summary
[Python] class, instance
"Kanrika" python class
Python tutorial summary
About python, class
#Python basics (scope)
python related summary
Python class, instance
#Python basics (class)
Python basics summary
python syslog wrapper class
Summary about Python scraping
Python class (Python learning memo ⑦)
Python Django tutorial summary
[Python] Class inheritance (super)
Python self-made class sort
[python] class basic methods
Summary about Python3 + OpenCV3
[Python] Class inheritance, override
Python function argument summary
python subprocess wrapper class
Python directory operation summary
Python AI framework summary
Python iteration related summary
Summary of Python arguments
YOLO Python wrapper class
Class notation in Python
Python exception class list
Python: Class and instance variables
Python3 programming functions personal summary
Summary of Python3 list operations
What's new in Python 3.10 (Summary)
C / C ++ programmer challenges Python (class)
Standard input / summary / python, ruby
Python web programming article summary
Python class variables and instance variables
python pandas study recent summary
Python Competitive Programming Site Summary
Python data type summary memo
Face detection summary in Python
What's new in Python 3.9 (Summary)
Python Crawling & Scraping Chapter 4 Summary
Virtual Environment Version Control Summary Python
How to write a Python class
perl objects and python class part 2.
Summary if using AWS Lambda (Python)
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Landmines hidden in Python class variables
R / Python coding rule link summary
A brief summary of Python collections
Machine learning summary by Python beginners
Python class definitions and instance handling
"The easiest Python introductory class" modified
selenium case study summary python pyCharm
Read PNG chunks in Python (class)
[Python] Road to snake charmer (3) Python class
Python package management tool personal summary
Python parallel / parallel processing sample code summary
Examine the object's class in python