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

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
S
SegmentFault 最新的问题
The Cloudflare Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
The Hacker News
The Hacker News
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
L
Lohrmann on Cybersecurity
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - 叶小钗
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
S
Secure Thoughts
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Help Net Security
Help Net Security
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Check Point Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
T
Tor Project blog
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
InfoQ
The Register - Security
The Register - Security
Recent Announcements
Recent Announcements
N
News and Events Feed by Topic
B
Blog RSS Feed

博客园 - 远洪

大模型常用术语 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)