Inner Functions - What Are They Good For?
Referece: https://realpython.com/blog/python/inner-functions-what-are-they-good-for/
Let’s look at three common reasons for writing inner functions.
Remember: In Python, a function is a “first-class” citizen, meaning they are on par with any other object (i.e., integers, strings, lists, modules, etc.). You can dynamically create or destroy them, pass them to other functions, return them as values, and so forth.
This tutorial utilizes Python version 3.4.1.
1. Encapsulation
You use inner functions to protect them from anything happening outside of the function, meaning that they are hidden from the global scope.
Here’s a simple example that highlights that concept:
|
Try calling the inner_increment() function:
|
Now comment out the inner_increment call and uncomment the outer function call, outer(10), passing in 10 as the argument:
|
Keep in mind that this is just an example. Although this code does achieve the desired result, it’s probably better to make the
inner_increment()function a top-level “private” function using a leading underscore:_inner_increment().
The following recursive example is a slightly better use case for a nested function:
|
Test this out as well. One main advantage of using this design pattern is that by performing all argument checking in the outer function, you can safely skip error checking altogether in the inner function.
For a more detailed discussion of recursion see, Problem Solving with Algorithms and Data Structures.
2. Keepin’ it DRY
Perhaps you have a giant function that performs the same chunk of code in numerous places. For example, you might write a function which processes a file, and you want to accept either an open file object or a file name:
|
Again, it is common to just make
do_stuff()a private top-level function, but if you want to hide it away as an internal function, you can.
How about a practical example?
Let’s say you want to know the number of WiFi hot spots in New York City. And yes the city has the raw data to tell us: datasource. Visit the site and download the CSV.
|
Run the function:
|
3. Closures and Factory Functions
Now we come to the most important reason to use inner functions. All of the inner function examples we’ve seen so far have been ordinary functions that merely happened to be nested inside another function. In other words, we could have defined these functions in another way (as discussed); there is no specific reason for why they should be nested.
But when it comes to closure, that is not the case: You must utilize nested functions.
What’s a closure?
A closure simply causes the inner function to remember the state of its environment when called. Beginners often think that a closure is the inner function, when it’s really caused by the inner function. The closure “closes” the local variable on the stack and this stays around after the the stack creation has finished executing.
An example
|
What’s happening here?
- The ‘generate_power()’ function is a factory function – which simply means that it creates a new function each time it is called and then returns the newly created function. Thus,
raise_twoandraise_threeare the newly created functions. - What does this new, inner function do? It takes a single argument,
power, and returnsnumber**power. Where does the inner function get the value of
numberfrom? This is where the closure comes into play:nth_power()gets the value ofpowerfrom the outer function, the factory function. Let’s step through this process:- Call the outer function:
generate_power(2) - Build the
nth_power()function which takes a single argumentpower - Take a snapshot of the state of
nth_power()which includespower=2 - Pass that snapshot into the
generate_power()function - Return the
nth_power()function
Put another way, the closure functions to “initialize” the number bar in the
nth_power()function and then returns it. Now, whenever you call that newly returned function, it will always see its own private snapshot that includespower=2.- Call the outer function:
Real World
How about a real world example?
|
This is a simplified function to check if a certain user has the correct permissions to access a certain page. You could easily modify this to grab the user in session to check if they have the correct credentials to access a certain route. Instead of checking if the user is just equal to ‘Admin’, you could query the database to check the permission then return the correct view depending on whether the credentials are correct or not.
Conclusion
The use of closures and factory functions is the most common, and powerful, use for inner functions. In most cases, when you see a decorated function, the decorator is a factory function which takes a function as argument, and returns a new function which includes the old function inside the closure. Stop. Take a deep breath. Grab a coffee. Read that again.
Put another way, a decorator is just syntactic sugar for implementing the process outlined in the generate_power() example.
I’ll leave you with an example:
|
If your code editor allows it, view the generate_power(exponent) and generate_power(number)functions side-by-side to illustrate the concepts discussed. (Sublime Text has Column View, for example).
If you have not coded the two functions, open the code editor and start coding. For new programmers, coding is a hands on activity, like riding a bike you just have to do it – and do it solo. So back to the task at hand. After you typed the code, you can now clearly see that the code is similar in that it produces the same results but there are differences. For those who have never used decorators, noting these differences will be the first step in understanding them if you venture down that path.
If you’d like to know more about this syntax and decorators in general, check out our Primer on Python Decorators. Comment below with questions.
Edits made by Derrick Kearney. Thank you!
Inner Functions - What Are They Good For?的更多相关文章
- asp.net MVC helper 和自定义函数@functions小结
asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...
- 【跟着子迟品 underscore】Array Functions 相关源码拾遗 & 小结
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- 【跟着子迟品 underscore】Object Functions 相关源码拾遗 & 小结
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)
一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...
- QM模块包含主数据(Master data)和功能(functions)
QM模块包含主数据(Master data)和功能(functions) QM主数据 QM主数据 1 Material Master MM01/MM02/MM50待测 物料主数据 2 Sa ...
- jQuery String Functions
In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...
- 2-4. Using auto with Functions
在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...
- [Python] Pitfalls: About Default Parameter Values in Functions
Today an interesting bug (pitfall) is found when I was trying debug someone's code. There is a funct ...
- Kernel Functions for Machine Learning Applications
In recent years, Kernel methods have received major attention, particularly due to the increased pop ...
- Execution Order of Event Functions
In Unity scripting, there are a number of event functions that get executed in a predetermined order ...
随机推荐
- linux 下 pip 安装教程
方法一: 下载文件 wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate 执行安装 python get-pip.py 这就 ...
- git log退出方法
英文状态下按Q
- 面向对象的Shell脚本
还记得以前那个用算素数的正则表达式吗?编程这个世界太有趣了,总是能看到一些即别出心裁的东西.你有没有想过在写Shell脚本的时候可以把你的变量和函数放到一个类中?不要以为这不可能,这不,我在网上又看到 ...
- ReactNative WebView组件详解
在开发Android的时候,一般我们会有一些加载网页的需求,或者执行一些JavaScript,我们都知道在Android中实现这个功能的控件是WebView,在ReactNative中也有实现此类需求 ...
- Chrome封掉不在chrome商店中的插件解决办法
添加chrome的管理模板,chrome.adm文件:下载地址:链接:http://pan.baidu.com/s/1c08st7i 密码: e811 然后进行如下的设置,重启chrome就可以解除封 ...
- 简单理解MapView 以及 设置 MKAnnotationView
MKMapView 相当于一个容器 .可以展示 MKAnnotationView.. 要使用它需要设置 数据源代理 _mapView.delegate = self; 它的数据源对象就是 符合 ...
- [转]:Android 5.0的调度作业JobScheduler
参考:http://blog.csdn.net/cuiran/article/details/42805057 增加 JobScheduler 的同时,去掉了几个广播, CONNECTIVITY_AC ...
- Android 开发之修改 app 的字体大小(老人模式)
新的需求(可参见 微信和QQ改变字体): app 字体不随着系统字体大小变化 app 设置中有设置字体大小的开关,变大以后,整个 app 字体变大. 解决方案:(字体需要采用 dp 为单位,不能使用 ...
- 在Android Studio 和 Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
面向对象:曾经使用过SVN的同学. (因为Git 它 可以说是双重的SVN (本地一个服务器,远程一个服务器)),提交代码要有两次步骤,先提交到本地服务器,再把本地服务器在提交到远程服务器. 所以连S ...
- 【Unity】11.7 布料
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 Unity提供了两种布料组件:交互布料(Interactive Cloth).蒙皮布料(Skinned Cloth).为 ...