In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 294
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.