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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 西康的博客

mysql information_schema库中COLUMNS 和 TABLES表字段详解 python - queue 队列模块 python - 修改系统环境变量 python - threading 多进程模块 python - socketserver python - 反射 python - socket 基础 python 序列化 字典 异常处理 elasticsearch RESTfull _cat api python - 面向对象编程基础知识 (进阶) python - 面向对象编程基础知识 python 文件操作 python re 模块 正则表达式 python configparser 模块 python logging 模块 python hashlib 模块 python sys 模块
类的成员,类的特殊方法
西康的博客 · 2018-04-11 · via 博客园 - 西康的博客

类的成员:

  在类中的私有字段和私有方法是不能被类意外的方法调用的。只能在类的内使用。

  在普通字段和方法前加两个下划线就可以把普通字段定义成私有字段或方法。

  在子类中,不能执行父类中的方法,不能获得父类的字段。示例:

class bar():
    def __init__(self):
        self.name = 'bar_name'
        self.__age = 18

    def get_age(self):
        return (self.__age)

class b(bar):
    def __init__(self):
        self.__b_age = 17
        super(b,self).__init__()


    def show(self):
        print ('class bar name is {}'.format(self.name))
        print ('__b_age is {}'.format(self.__b_age))
        # print ('__age is {}'.format(self.__age))
        self.bar__age = super(b,self).get_age()
        print (self.bar__age)

obj = b()
obj.show()
# class bar name is bar_name
# __b_age is 17
# 18

类中的特殊成员

class bar():
    def __init__(self):
        print ('bar __init__ month!')
    def __call__(self, *args, **kwargs):
        print ('__call__ month')
    def __int__(self):
        # default int number.
        return 1
    def __str__(self):
        return 'string value'

obj = bar()
obj()
int(obj)
# str(obj)
print (obj)
# bar __init__ month!
# __call__ month
# string value

obj = bar() 执行 __init__ 方法

obj() 执行 __call__ 方法

int(obj) 执行 __init__ 方法

print (obj) 执行 __str__ 方法

 类中比较重要的内部方法:

__getitem__  -> obj['test']

__setitem__ -> obj[213] = 98

__delitem__ -> del obj['test_key']

class bar():
    def __init__(self):
        print ('bar __init__ month!')
    def __getitem__(self, item):
        return ('getitem index value is: {}'.format(item))
    def __setitem__(self, key, value):
        # default int number.
        print ('{} = {}'.format(key,value))
    def __delitem__(self, key):
        print ('is delitem values: {}'.format(key))

obj = bar()
print (obj['test'])
obj[213] = 98
del obj['test_key']
# bar __init__ month!
# getitem index value is: test
# 213 = 98
# is delitem values: test_key

 25day类成员方法切片

test