Python中静态方法的实现
作者:老王
Python似乎很讨厌修饰符,没有常见的static语法。其静态方法的实现大致有以下两种方法:
第一种方式(staticmethod):
>>> class Foo:
str = "I'm a static method."
def bar():
print Foo.str
bar = staticmethod(bar)
>>> Foo.bar()
I'm a static method.
第二种方式(classmethod):
>>> class Foo:
str = "I'm a static method."
def bar(cls):
print cls.str
bar = classmethod(bar)
>>> Foo.bar()
I'm a static method.
---------------------------------------------------------------
上面的代码我们还可以写的更简便些:
>>> class Foo:
str = "I'm a static method."
@staticmethod
def bar():
print Foo.str
>>> Foo.bar()
I'm a static method.
或者
>>> class Foo:
str = "I'm a static method."
@classmethod
def bar(cls):
print cls.str
>>> Foo.bar()
I'm a static method.
OK,差不多就是这个样子了。
相关文档:
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......
运行一句python命令
对vc设置路径
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
Py_Initialize() ;
PyRun_SimpleString("print('Hello')");
//PyRun_SimpleString("print(dir())");
Py_Finalize();& ......
本篇将介绍python中sys, getopt模块处理命令行参数
如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?
需要模块:sys
参数个数:len(sys.argv)
脚本名: sys.argv[0]
参数1: sys.argv[1]
参数2: sys.argv[2]
test.py
1
import ......
这个脚本是在 python 环境下使用的,改的网上的一个脚本,可以检测代理中国(www.proxycn.com)上的HTTP代理列表,你也可以自己去上面找列表检测 代码: #!/usr/bin/python # -*- coding: utf-8 -*- # from: ubuntu.org.cn Copyright: GPLv2 import urllib import re from datetime import datetime import socket def fin ......
找了半天没找着,终于在英文站点上找到,还有感谢群里的石头和球迷
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
别外付一个python对时间的一些函数,很好用的
我们先导入必须用到的一个module
>>> import time
设置一个时间的格式,下面会用到
& ......