Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
feilongluo
explore-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
explore-python
/
Class
/
slots.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (40 loc) · 2.05 KB
master
Breadcrumbs
explore-python
/
Class
/
slots.md
Copy path
Top
File metadata and controls
Code
Blame
58 lines (40 loc) · 2.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#
slots 魔法
在 Python 中,我们在定义类的时候可以定义属性和方法。当我们创建了一个类的实例后,我们还可以给该实例绑定任意新的属性和方法。
看下面一个简单的例子:
```
python
class
Point
(
object
):
def
__init__
(
self
,
x
=
0
,
y
=
0
):
self
.x
=
x
self
.y
=
y
>>
>
p
=
Point(
3
,
4
)
>>
>
p.z
=
5
#
绑定了一个新的属性
>>
>
p.z
5
>>
>
p.
__dict__
{
'
x
'
:
3
,
'
y
'
:
4
,
'
z
'
:
5
}
```
在上面,我们创建了实例 p 之后,给它绑定了一个新的属性 z,这种动态绑定的功能虽然很有用,但它的代价是消耗了更多的内存。
因此,为了不浪费内存,可以使用
`
__slots__
`
来告诉 Python 只给一个固定集合的属性分配空间,对上面的代码做一点改进,如下:
```
python
class
Point
(
object
):
__slots__
=
(
'
x
'
,
'
y
'
)
#
只允许使用 x 和 y
def
__init__
(
self
,
x
=
0
,
y
=
0
):
self
.x
=
x
self
.y
=
y
```
上面,我们给
`
__slots__
`
设置了一个元组,来限制类能添加的属性。现在,如果我们想绑定一个新的属性,比如 z,就会出错了,如下:
```
python
>>
>
p
=
Point(
3
,
4
)
>>
>
p.z
=
5
--------------------------------------------------------------------------
-
AttributeError
Traceback (most recent call last)
<
ipython
-
input
-
648
-
625ed954d865
>
in
<
module
>
()
----
>
1
p.z
=
5
AttributeError
:
'
Point
'
object
has no attribute
'
z
'
```
使用
`
__slots__
`
有一点需要注意的是,
`
__slots__
`
设置的属性仅对当前类有效,对继承的子类不起效,除非子类也定义了
`
__slots__
`
,这样,子类允许定义的属性就是自身的 slots 加上父类的 slots。
#
小结
-
__
slots
__
魔法:限定允许绑定的属性.
-
`
__slots__
`
设置的属性仅对当前类有效,对继承的子类不起效,除非子类也定义了 slots,这样,子类允许定义的属性就是自身的 slots 加上父类的 slots。
#
参考资料
-
[
slots魔法 · Python进阶
]
(
https://eastlakeside.gitbooks.io/interpy-zh/content/slots_magic/
)
You can’t perform that action at this time.