Home  >  Article  >  Backend Development  >  Briefly understand python private and public attributes

Briefly understand python private and public attributes

怪我咯
怪我咯Original
2017-06-23 13:55:541488browse

In python, variables outside methods within a class are called attributes, and variables within methods within a class are called fields. Their private and public access methods are similar.

class C:
    __name="私有属性"

    def func(self):
        print(C.__name)

class sub_C(C):
    def info(self):
        print(C.__name)#派生类中不可以访问父类的私有字段
obj=C()
obj.func()
obj=sub_C()
obj.info()

The access to methods and properties is similar to the above method, that is: private members can only be used inside the class

The above is the detailed content of Briefly understand python private and public attributes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn