Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does the privatization of Python mean?

Shulou Source: shulou.com Published: 2022-06-02 04:39:05 09月19日 Update

This article mainly introduces "what is the meaning of Python privatization". In daily operation, I believe many people have doubts about what Python privatization means. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what does Python privatization mean?" Next, please follow the editor to study!

Xx: public variable

_ x: single pre-underlined, privatized properties or methods, from somemodule import * prohibited import, class objects and subclasses can be accessed

_ _ xx: double pre-underlined to avoid conflicts with attribute names in subclasses and cannot be accessed directly outside (the name is renamed so that it cannot be accessed)

Xx: a magic object or attribute that is underlined before and after a user's namespace. For example: _ _ init__, _ _ don't invent such a name yourself

Xx_: single underlined to avoid conflicts with Python keywords

Private can be accessed through the name mangling (name renaming (the purpose is to prevent subclasses from accidentally overriding methods or properties of the base class) such as: _ Class__object).

# coding=utf-8class Person (object): def _ _ init__ (self, name, age) Taste): self.name = name self._age = age self.__taste = taste def showperson (self): print (self.name) print (self._age) print (self.__taste) def dowork (self): self._work () self.__away () def _ work (self): print ('my _ work) ') def _ away (self): print (' my _ away') class Student (Person): def construction (self) Name, age, taste): self.name = name self._age = age self.__taste = taste def showstudent (self): print (self.name) print (self._age) print (self.__taste) @ staticmethod def testbug (): _ Bug.showbug () # can be accessed within the module When from cur_module import * is not imported, class _ Bug (object): @ staticmethod def showbug (): print ("showbug") S1 = Student ('jack', 25,' football') s1.showperson () print ('*'* 20) # cannot access _ _ taste, resulting in an error # s1.showstudent () s1.construction ('rose', 30,' basketball') s1.showperson () print ('* * 20) s1.showstudent () print ('* * 20) Student.testbug ()

[failed to upload picture... (image-c0ee83-1521130952917)]

Summary

If the attribute name in the parent class is _ _ name, the child class does not inherit and the child class cannot access it.

If you assign a value to the _ _ name in a subclass, an attribute with the same name as the parent class is defined in the subclass

Variables, functions and classes with _ name will not be imported when using from xxx import *

At this point, the study on "what is the meaning of Python privatization" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Name subclass attribute private meaning underscore learning method variable object more conflict help utility same accident next key keyword function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB NVidia Apple vpn