python中的函数存入list中的实例
最近由于接触了python这个强大的东西,在写代码时考虑到代码的扩展性,就想到了将python的函数名存入list中。有点像习惯的c/c++中的函数指针的意思。
下面上代码:
- # coding=utf-8
- #!/usr/bin/python
- #脚本的用法 python nn_daemon.py
- import json, urllib, urllib2, subprocess, sys, os, logging, time, socket, time, calendar, datetime, ConfigParser, socket, fcntl, struct
- from xml.etree import ElementTree
- from time import strftime
- def func_1(i):
- print "i is %d"%i
- def func_2(i):
- print "i is %d"%i
- if __name__ == '__main__':
- tmp_list = []
- tmp_list.append(func_1)
- tmp_list.append(func_2)
- for i in range(len(tmp_list)):
- tmp_list[i](i)
结果:
i is 0
i is 1
本菜鸟是python新手,望各种喷。后面会陆续把最近碰到的python问题分享出来。
python中的函数存入list中的实例的更多相关文章
- python之BIF函数在列表中的应用
1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 T ...
- shell中的函数、shell中的数组、告警系统需求分析
7月16日任务 20.16/20.17 shell中的函数20.18 shell中的数组20.19 告警系统需求分析 20.16/20.17 shell中的函数 函数就是一个子shell就是一个代码段 ...
- Linux centosVMware shell中的函数、shell中的数组、
一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...
- JavaScript中的函数和C#中的匿名函数(委托、lambda表达式)
在js中function是一个一个引用类型,所以可以出现这样的代码: 'use strict'; var compare=function(value1, value2) { if (value1&l ...
- C++结构体中使用函数与类中使用函数小结
#include <iostream>#include <string.h>using namespace std;struct stud//学生信息结构体{ char ...
- 解决python中write()函数向文件中写中文时出现乱码的问题
今天看<python编程从入门到实践>的第10章文件.异常,在做练习的时候,向文件中写内容,但是写中文就不行,后来在百度上查了众多资料,解决方法如下: 解决:在open()函数中添加一个e ...
- python内置函数:sorted中的参数key
x.sort和sorted函数中参数key的使用 介绍 python中,列表自带了排序函数sort >>> l = [1, 3, 2] >>> l.sort() & ...
- python在读取配置文件存入列表中,去掉回车符号
self.receiver = map(lambda x: x.strip(), receiver_list) # 去掉list中的回车符号
- python中range函数与列表中删除元素
一.range函数使用 range(1,5) 代表从1到4(不包含5),结果为:1,2,3,4 ,默认步长为1 range(1,5,2) 结果为:1, 3 (同样不包含5) ,步长为2 ...
随机推荐
- .Net SSRS(rdlc) 报表经验总结
排版 1. 可以利用表格来布局,以避免调整固定宽度的麻烦. 2. 一个表的表头里还可以嵌套表格. 3. 设置rdlc报表打印格式.首先打开RDLC报表设计器页面.在灰色部分点右键 -> 报表属性 ...
- DataTables给表格绑定事件
$(document).ready(function() { $('#example').dataTable(); $('#example tbody').on('click', 'tr', func ...
- 同时处理html+js+jquery+css的插件安装(Spket&Aptana插件安装)
Spket 在线安装方法:Help->Software Updates(或者Install New Software)->Add site Location:http://www.spke ...
- cs代码实现控件移动TranslateTransform
xaml: <Rectangle> <Rectangle.RenderTransform> <TranslateTransform x:Name="myTran ...
- 防止DC电源反接的方法——SS14的用法
出处:http://blog.ednchina.com/tengjingshu 电源是PCB板的重要部分,每个芯片都需要电源供给.芯片其实是挺脆弱的,只要正负接反得话,大多数就会挂掉,相信很多人都有惨 ...
- 【转】使用 vim + ctags + cscope + taglist 阅读源码
原文网址:http://my.oschina.net/u/554995/blog/59927 最近,准备跟学长一起往 linux kernel 的门里瞧瞧里面的世界,虽然我们知道门就在那,但我们还得找 ...
- javascript笔记2之数据类型
/* var box; alert(typeof box); //box是Undefined类型,值是undefined,类型返回的字符串是undefined var box = true; aler ...
- WPF 实现控件间拖拽内容
想实现这样一个常用功能:在ListBox的一个Item上点住左键,然后拖拽到另外一个控件(如ListView中),松开左键,数据已经拖拽过来. 步骤如下: 1. 设置ListBox 的AllowDro ...
- AJAX上传文件
function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
权限问题,授权 给 root 全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...