惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
L
LINUX DO - 最新话题
博客园_首页
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
C
Cisco Blogs
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
A
About on SuperTechFans
G
GRAHAM CLULEY
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
D
DataBreaches.Net
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
I
InfoQ
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 叶小钗
Project Zero
Project Zero

博客园 - 远洪

大模型常用术语 windows 使用sshAgent 加载秘钥 再次认识java反射 再次认识java注解 再次认识java泛型 java中类的分类 java类中的成员变量,静态变量与局部变量 再谈java枚举enum 使用CCProxy让手机访问电脑能访问的网址 playwright启动后报错net::ERR_CERT_COMMON_NAME_INVALID 解决方法 debian 或ubuntu安装使用tigervnc python 实例属性、类属性、实例方法、类方法、静态方法 docker compose使用 docker 自定义网络 Dockerfile 使用 golang进程(主线程)与协程 go语言多态中的类型断言 java中的多态与golang中的多态 golang 定义接口
python面向对象封装,私有变量
远洪 · 2024-03-30 · via 博客园 - 远洪

一、私有变量

“_”单下划线开头的变量:_var  ,表示这些函数和变量是元素私有的或内部使用的,为非强制性(实际可修改也可引用)。

"__"双下划线开头的变量:__var,表示这些函数和变量是元素私有的或内部使用的,为强制性,对象外不可修改或引用。

单下划线例子

class People():
    def __init__(self,name,age):
        self._name = name
        self._age = age

peo = People("aa",22)
print(peo.__dict__)
print(peo._name)
print(peo._age)

双下划线例子

class People():
    def __init__(self,name,age):
        self.__name = name
        self.__age = age

peo = People("aa",22)
print(peo.__dict__)
print(peo.__name)
print(peo.__age)

 二、通过函数获取或修改私有变量

class People():
    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    def get_name(self):
        return self.__name
    def set_name(self,name):
        self.__name = name

peo = People("aa",22)
print(peo.__dict__)
print(peo.get_name())
peo.set_name("bb")
print(peo.get_name())

 三、使用装饰器将函数变为变量,来封装对私有变量的修改

class People():
    def __init__(self,name,age):
        self.__name = name
        self.__age = age

    # 通过该装饰器修饰后,该函数可以像变量一样使用
    @property
    def name(self):
        print("通过函数获取变量")
        return self.__name

    # 通过该装饰器修饰后,该函数可以像变量一样使用
    @name.setter
    def name(self,name):
        if self.__name == name:
            raise Exception("不能和原来的名字相同")
        else:
            self.__name = name

peo = People("aa",22)
print(peo.__dict__)
print(peo.name)
peo.name = "aa"
print(peo.name)