Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
learn-python/src/classes/test_method_objects.py at master · arianshi/learn-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
arianshi
learn-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learn-python
/
src
/
classes
/
test_method_objects.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
79 lines (64 loc) · 4.2 KB
master
Breadcrumbs
learn-python
/
src
/
classes
/
test_method_objects.py
Copy path
Top
File metadata and controls
Code
Blame
79 lines (64 loc) · 4.2 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Class Definition Syntax.
类定义语法 (Class Definition Syntax)
@see: https://docs.python.org/3/tutorial/classes.html#method-objects
"""
class
MyCounter
:
"""A simple example of the counter class"""
counter
=
10
def
get_counter
(
self
):
"""Return the counter"""
# 返回计数器
return
self
.
counter
def
increment_counter
(
self
):
"""Increment the counter"""
# 让计数器自增 1
self
.
counter
+=
1
return
self
.
counter
def
test_method_objects
():
"""Method Objects."""
# 方法对象。
# The other kind of instance attribute reference is a method. A method is a function that
# “belongs to” an object. (In Python, the term method is not unique to class instances: other
# object types can have methods as well. For example, list objects have methods called append,
# insert, remove, sort, and so on. However, in the following discussion, we’ll use the term
# method exclusively to mean methods of class instance objects, unless explicitly stated
# otherwise.)
# 另一种实例属性引用是方法 (method)。方法是"属于"某个对象的函数。(在 Python 中,
# method 这个术语并不只用于类的实例:其他对象类型也可以有方法。例如,列表对象有
# append、insert、remove、sort 等方法。不过,在下面的讨论中,除非另有说明,
# 我们用 method 这个词专指类实例对象的方法。)
# But be aware that counter.get_counter() is not the same thing as MyCounter.get_counter() —
# it is a method object, not a function object.
# 但要注意 counter.get_counter() 与 MyCounter.get_counter() 并不是一回事 ——
# 前者是一个方法对象 (method object),而不是函数对象 (function object)。
# Usually, a method is called right after it is bound
# 通常情况下,方法在被绑定之后会立刻被调用
counter
=
MyCounter
()
assert
counter
.
get_counter
()
==
10
# However, it is not necessary to call a method right away: counter.get_counter() is a method
# object, and can be stored away and called at a later time. For example:
# 然而,并不一定要立刻调用方法:counter.get_counter() 是一个方法对象,可以先把它
# 存起来,稍后再调用。例如:
get_counter
=
counter
.
get_counter
assert
get_counter
()
==
10
# What exactly happens when a method is called? You may have noticed that counter.get_counter()
# was called without an argument above, even though the function definition for get_counter()
# specified an argument (self). What happened to the argument? Surely Python raises an
# exception when a function that requires an argument is called without any — even if the
# argument isn’t actually used…
# 当方法被调用时,到底发生了什么?你可能已经注意到,上面调用 counter.get_counter()
# 时没有传入任何参数,尽管 get_counter() 的函数定义中指定了一个参数(self)。
# 那个参数去哪儿了?毕竟,当一个需要参数的函数在没有传入任何参数时被调用,
# Python 一定会抛出异常 —— 即便那个参数实际上并没有被使用……
# Actually, you may have guessed the answer: the special thing about methods is that the
# instance object is passed as the first argument of the function. In our example, the call
# counter.get_counter() is exactly equivalent to MyCounter.get_counter(counter). In general,
# calling a method with a list of n arguments is equivalent to calling the corresponding
# function with an argument list that is created by inserting the method’s instance object
# before the first argument.
# 其实你可能已经猜到答案了:方法的特别之处在于,实例对象会作为函数的第一个参数
# 被传入。在我们的例子中,counter.get_counter() 这次调用完全等价于
# MyCounter.get_counter(counter)。一般来说,用 n 个参数调用一个方法,等价于
# 调用对应的函数,并把方法的实例对象插到参数列表的第一个位置之前。
assert
counter
.
get_counter
()
==
10
assert
MyCounter
.
get_counter
(
counter
)
==
10
You can’t perform that action at this time.