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

推荐订阅源

Cyberwarzone
Cyberwarzone
S
Secure Thoughts
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
A
Arctic Wolf
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
U
Unit 42
Recorded Future
Recorded Future
Hacker News: Ask HN
Hacker News: Ask HN
F
Full Disclosure
Spread Privacy
Spread Privacy
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Know Your Adversary
Know Your Adversary
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
T
Threatpost
I
InfoQ
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cloudbric
Cloudbric
L
LangChain Blog
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
S
Security @ Cisco Blogs
B
Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
Simon Willison's Weblog
Simon Willison's Weblog
P
Proofpoint News Feed
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
W
WeLiveSecurity
Forbes - Security
Forbes - Security
O
OpenAI News
C
Check Point Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Last Watchdog
The Last Watchdog
H
Help Net Security
SecWiki News
SecWiki News

博客园 - 远洪

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