python自定义函数在Python解释器中调用
https://docs.python.org/2.7/tutorial/modules.html Modules
If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program. To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents: # Fibonacci numbers module def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
Now enter the Python interpreter and import this module with the following command: >>>
>>> import fibo
This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions: >>>
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'
If you intend to use a function often you can assign it to a local name: >>>
>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377
http://www.zhihu.com/question/25577999
python自定义函数在Python解释器中调用的更多相关文章
- mysql自定义函数并在存储过程中调用,生成一千万条数据
mysql 自定义函数,生成 n 个字符长度的随机字符串 -- sql function delimiter $$ create function rand_str(n int) returns VA ...
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- Python的函数名作为参数传入调用以及map、reduce、filter
零.python的lambda函数: #lambda function func = lambda x : x+1 #这里是一个匿名函数,x是参数,x+1是对参数的操作 func(1)= 2 多个参数 ...
- 17.python自定义函数
什么是函数,函数说白了就是将一系列代码封装起来,实现代码的重用. 什么是代码重用? 假设我有这样的需求: 但是我还是觉得太麻烦了,每次想吃饭的时候都要重复这样的步骤.此时,我希望有这样的机器:
- python 自定义函数
200 ? "200px" : this.width)!important;} --> 介绍 在Python中,定义一个函数要使用def语句,依次写出函数名.括号.括号中的参 ...
- apiAutoTest:支持自定义函数,用例中可调用
0. 前言 apiAutoTest从去年8月以来开源至今,也更新了不少内容,一起来看看吧 第一个版本 - 2020/08/08 增加实际响应存储数据的方法,并在字典可以处理依赖见tools/svae_ ...
- Python学习教程(learning Python)--2.3 Python自定义函数传参函数设计
Python里自定义子函数时,可以在调用时携带一些参数到子函数里去处理.具体用法结构如下: def func(arguments): statement statement etc. 定义子函数一定要 ...
- python自定义函数和推导
#之所以把这俩写一起,并不是因为这俩有什么关系,因为都太简单,没什么可说的 #自定义函数的格式,def开头,后面空格,在后面是函数名,接括号,括号里是入参参数 #!/usr/bin/python # ...
- 【python】函数名存在变量中
变量函数:意思就是将函数名存在变量中,然后根据变量值动态的调用需要的函数. LOGIN = 'xxxx' PASSWD = "xxx" URL = 'xxxxx' def hand ...
随机推荐
- js设计模式(8)---享元模式
0.前言 今天总结了四种设计模式,到现在有点精疲力尽了,但是还是有不少收获,很开心自己有掌握了新的东西,今天变得有了价值. 1.使用条件 1.1.网页中使用了大量资源密集型的对象: 1.2.这些对象中 ...
- 【Qt】Qt之自定义界面(QMessageBox)【转】
简述 通过前几节的自定义窗体的学习,我们可以很容易的写出一套属于自己风格的界面框架,通用于各种窗体,比如:QWidget.QDialog.QMainWindow. 大多数窗体的实现都是采用控件堆积来完 ...
- net 中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net) (转)
作者:王先荣前言 随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提供了VFW.DirectShow和MediaFoundation这 三代接口.其中VFW早已被Di ...
- Redis源码研究--双向链表
之前看的内容,占个位子,以后补上. ----------8月4日--------------- 双向链表这部分看的比较爽,代码写的中规中矩,心里窃喜,跟之前学的<数据结构>这本书中差不多. ...
- 重拾C,一天一点点
数据类型及长度 char 字符型,占用一个字节 int 整型,通常代表特定机器中整数的自然长度 short 16位 int 16位或32位 ...
- js中(function(){…})()立即执行函数写法理解(转载oschina)
( function(){…} )()和( function (){…} () )是两种javascript立即执行函数的常见写法,最初我以为是一个括号包裹匿名函数,再在后面加个括号调用函数,最后达到 ...
- ajax 设置Access-Control-Allow-Origin实现跨域访问
ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jquery的jsonp方法,t ...
- 使用Animation实现Button的透明度Opacity变化
接着之前的使Button的Content变化的例子,这里给出使Button的透明度变化的写法. 前台写法: 后台写法: 效果图:Opacity的值正在变小 效果还不错,前台是用Blend生成的,后台的 ...
- java 简单搜索算法
--无序查找 public static int Search(int[] a,int key){ for(int i=0;i<a.length;i++){ if(key==a[i]){ ret ...
- iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):2 H264数据写入文件
本文档为iOS VideoToolbox硬编H.265(HEVC)H.264(AVC):1 概述续篇,主要描述: CMSampleBufferRef读取实际数据 序列参数集(Sequence Para ...