Home > Backend Development > Python Tutorial > Briefly understand python private and public attributes

Briefly understand python private and public attributes

怪我咯
Release: 2017-06-23 13:55:54
Original
1536 people have browsed it

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()
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template