Comments (7)
if user.no_profile_exists: +++ profile_func = create_user_profile extra_args = {'points': 0, 'created': now()} else: +++ profile_func = update_user_profile extra_args = {'updated': now()} profile_func( username=user.username, email=user.email, age=user.age, address=user.address, **extra_args )
你好,上面代码中我标记了 “+++” 的两行可以改成下面这样的吗?
create_user_profile = profile_func update_user_profile = profile_func
这样肯定是不行的。在python中函数也是一种对象,它能被赋值给变量,故调用该变量就是执行该函数。感觉你对这个理解有些不清晰,具体可以看廖雪峰的教程开头的例子。作者在这里是巧妙地通过给同一个变量赋予不同的值来执行了不同的函数
from one-python-craftsman.
感谢帮忙解答,先 close 了 :)
from one-python-craftsman.
我理解的是:把函数赋值给变量,应该是 变量=函数名,如果是 函数名=变量,而变量之前并没有被定义,那不就报错了吗?
from one-python-craftsman.
你的理解没有错,只是对代码可能有误解,create_user_profile
和update_user_profile
是实际被调用的函数,这里通过条件判断选择需要调用哪个函数,最后使用profile_func
来调用选择要调用的函数。
from one-python-craftsman.
我在 Jupyter 里运行了一下,profile_func = create_user_profile,profile_func = update_user_profile
和create_user_profile = profile_func,update_user_profile = profile_func
的结果是一样的,但我还是感觉要写成后者那样,或者说这两种写法有什么不同吗?
from one-python-craftsman.
扩展了一下原文里的代码,这样可能更容易理解一些。
只有create_user_profile
和update_user_profile
是实际实现功能的函数,在条件判断代码里,根据user.no_profile_exists
的值来判断是应该调用create_user_profile
还是update_user_profile
。此时,profile_func只是指向将要调用的一个函数的变量。
def now():
return 100
def create_user_profile(username, email, age, address, points, created):
""" code to create a new user profile """
print("creating user")
def update_user_profile(username, email, age, address, updated):
""" code to update a existing user profile """
print("updating user")
if True:
profile_func = create_user_profile
extra_args = {'points': 0, 'created': now()}
else:
profile_func = update_user_profile
extra_args = {'updated': now()}
# either create or update a user profile, no matter it already exists or not
profile_func(
username='user.username',
email='user.email',
age='user.age',
address='user.address',
**extra_args
)
如果在你标出的位置使用create_user_profile = profile_func
,update_user_profile = profile_func
,此时profile_func根本不能存在,是不可能调到相应的函数的,会报错NameError: name 'profile_func' is not defined
。
我在Jupyter里面也运行了,的确不会报错,但是也的确没有按照期望调用到相应的函数。原因是Jupyter的执行环境有上下文的,如果你先运行了正确的代码,再执行你的代码,实际上profile_func
是在之前的上下文被定义了,所以执行结果一样吧。
贴了我测试的代码,你可以尝试在你想要修改的地方修改再运行试试,注意,我直接用了True/False来控制调用哪个函数,意思差不多。最好别用Jupyter,问题就很明确了。
from one-python-craftsman.
谢谢,明白了
from one-python-craftsman.
Related Issues (20)
- 第六篇-上下文管理器改善异常处理流程的疑问 HOT 1
- blog服务好像挂了.https://www.zlovezl.cn/ HOT 5
- Python suffixed of some code block is missing HOT 1
- To add software craftsmanship manifesto?
- 縮進問題 HOT 1
- 微信公众号图裂了 HOT 1
- 使用namedtuple可以用解包赋值给多个变量 HOT 1
- 推荐添加用在装饰器上的自省函数 inspect.signature 的使用说明 HOT 1
- 第六篇文章“Write Cleaner Python: Use Exceptions”链接已失效 HOT 1
- 现已支持switch case HOT 2
- 关于「使用 try/while/for 中 else 分支」 HOT 1
- 写的很棒 HOT 1
- OOP 原则 HOT 2
- 书籍建议 HOT 3
- 有关Monkey Patch HOT 1
- 是否有epub电子书购买渠道? HOT 2
- On Code Repo HOT 2
- On Code in 10-a-good-player-know-the-rules.md HOT 1
- 类型注解来声明类型 HOT 1
- 关于异常处理代码对照示例的疑问
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from one-python-craftsman.