首页 > 代码库 > pyhon基础
pyhon基础
1. def fun:定义函数
def _initialize(self, key=16*‘\0‘, iv=16*‘\0‘):
....
其中self相当于c/c++ this指针,可以不写,默认自带
2. pass用法:
表示它不做任何事情,一般用做占位语句
//在c/c++中if (a > 4) { //do nothing} else { printf("less than 4") }//在python中if a > 4 : pass #do nothingelse: print "less than 4"
3. isinstance(实例化对象,类)作用: 判断实例化对象是不是该类实例化的,是返回True,不是返回False
class Base:
pass
b = Base()
print isinstance(b, Base)
4. python 字符串操作:http://www.cnpythoner.com/wiki/string.html
‘ ’.join("abcd")
5. python异常处理:http://hanyuejuan.blog.163.com/blog/static/52929232201301844912530/
try..except
try...finally
raise
自定义异常
6. python文件的写法:
method1. 第一行引入:#!/usr/bin/env /usr/bin/python
method2. 可以不写method1这句话,可以直接在命令行调用pyhon,然后import 该py文件
7. ctypes: python foreign library.用来兼容c数据类型和调用dll/so文件的函数,同时把他们包装成纯粹的python库:https://docs.python.org/2/library/ctypes.html#accessing-functions-from-loaded-dlls
1. load dynamc libraries
>>> cdll.LoadLibrary("libc.so.6") <CDLL ‘libc.so.6‘, handle ... at ...>>>> libc = CDLL("libc.so.6") >>> libc <CDLL ‘libc.so.6‘, handle ... at ...>>>>
2. call function
print libc.time(None)
3. ctype类型
ctypes type | C type | Python type |
---|---|---|
c_bool | _Bool | bool (1) |
c_char | char | 1-character string |
c_wchar | wchar_t | 1-character unicode string |
c_byte | char | int/long |
c_ubyte | unsigned char | int/long |
c_short | short | int/long |
c_ushort | unsigned short | int/long |
c_int | int | int/long |
c_uint | unsigned int | int/long |
c_long | long | int/long |
c_ulong | unsigned long | int/long |
c_longlong | __int64 or long long | int/long |
c_ulonglong | unsigned __int64 or unsigned long long | int/long |
c_float | float | float |
c_double | double | float |
c_longdouble | long double | float |
c_char_p | char * (NUL terminated) | string or None |
c_wchar_p | wchar_t * (NUL terminated) | unicode or None |
c_void_p | void * | int/long or None |
>>> i = c_int(42)>>> print ic_long(42)>>> print i.value42>>> i.value = http://www.mamicode.com/-99"color: #0000ff;">print i.value-99
multable memory blocks:
>>>from ctypes import *>>>c_s = c_char_p("hello")>>>print c_s.valuehello>>>p = c_s>>>print p.valuehello>>>p.value=http://www.mamicode.com/"aaaa">>>print p.value c_s.value"aaaa" "hello"#结论:Python strings are immutable>>>from cypes import *>>>p = create_string_buffer( "aaa")>>>print sizeof(p), repr(p.raw)4‘aaa\x00‘>>>c_s = p>>>print c_s.valueaaa>>> c_s.value=http://www.mamicode.com/"bbb">>>print c.s_value p.valuebbb bbb#结论:If you need mutable memory blocks, ctypes has a create_string_buffer() function which creates these in various ways.
8.ord
chr()根据整数返回对应的字符,也就是讲ascii转换为字符
unichr()将整数返回成unicode字符
ord()将字符转换成ascii码
8. repr
9.导入动态库方法
library_path = ‘C:/Python27/Lib/site-packages/CryptoMobile/‘
library_suf = ‘.so‘
library_name = [‘SNOW_3G‘, ‘Kasumi‘, ‘ZUC‘]
def load_lib(name=None):
if name == None:
libraries = [‘‘.join((library_path, lib, library_suf)) for lib in library_name]
return map(cdll.LoadLibrary, libraries)
elif isinstance(name, str) and name in library_name:
return cdll.LoadLibrary(‘‘.join((library_path, name, library_suf)))
__func_Initialization = lib.Initiallization#调用lib库里面的函数
__func_Initialization.argtypes = [POINTER(c_ubyte), POINTER(c_ubyte)]
__func_Initialization.restype= None
10. __init__
相当于c++的构造函数
class
Person
:
def
__init__
(self, name):
self.name = name def
sayHi
(self):
print
‘Hello, my name is‘
, self.name
p = Person(
‘Swaroop‘
)
p.sayHi()
11. Python中filter、map、reduce、lambda 的用法
pyhon基础