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 }}
djc-github
explore-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
explore-python
/
Class
/
method.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
63 lines (44 loc) · 2.03 KB
master
Breadcrumbs
explore-python
/
Class
/
method.md
Copy path
Top
File metadata and controls
Code
Blame
63 lines (44 loc) · 2.03 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
57
58
59
60
61
# 类方法和静态方法
在讲类方法和静态方法之前
,
先来看一个简单的例子
:
``
`
python
class
A
(
object
):
def
foo
(
self
):
print
'Hello '
,
self
>
>>
a
=
A
()
>
>>
a
.
foo
()
Hello
,
<
__main__
.
A
object
at
0x10c37a450
>
``
`
在上面
,
我们定义了一个类
A
,
它有一个方法
foo
,
然后我们创建了一个对象
a
,
并调用方法
foo
。
## 类方法
如果我们想通过类来调用方法
,
而不是通过实例
,
那应该怎么办呢
?
Python
提供了
`classmethod`
装饰器让我们实现上述功能
,
看下面的例子
:
``
`
python
class
A
(
object
):
bar
=
1
@
classmethod
def
class_foo
(
cls
):
print
'Hello, '
,
cls
print
cls
.
bar
>
>>
A
.
class_foo
()
# 直接通过类来调用方法
Hello
,
<
class
'__main__.A'
>
1
``
`
在上面
,
我们使用了
`classmethod`
装饰方法
`class_foo`
,
它就变成了一个类方法
,
`class_foo`
的参数是
cls
,
代表类本身
,
当我们使用
`A.class_foo()`
时
,
cls
就会接收
A
作为参数
。
另外
,
被
`classmethod`
装饰的方法由于持有
cls
参数
,
因此我们可以在方法里面调用类的属性
、
方法
,
比如
`cls.bar`
。
## 静态方法
在类中往往有一些方法跟类有关系
,
但是又不会改变类和实例状态的方法
,
这种方法是
**
静态方法
**
,
我们使用
`staticmethod`
来装饰
,
比如下面的例子
:
``
`
python
class
A
(
object
):
bar
=
1
@
staticmethod
def
static_foo
():
print
'Hello, '
,
A
.
bar
>
>>
a
=
A
()
>
>>
a
.
static_foo
()
Hello
,
1
>>
>
A
.
static_foo
()
Hello
,
1
``
`
可以看到
,
静态方法没有
self
和
cls
参数
,
可以把它看成是一个普通的函数
,
我们当然可以把它写到类外面
,
但这是不推荐的
,
因为这不利于代码的组织和命名空间的整洁
。
# 小结
-
类方法使用
`@classmethod`
装饰器
,
可以使用类
(
也可使用实例
)
来调用方法
。
-
静态方法使用
`@staticmethod`
装饰器
,
它是跟类有关系但在运行时又不需要实例和类参与的方法
,
可以使用类和实例来调用
。
You can’t perform that action at this time.