导航:首页 > 编程语言 > python代码封装函数

python代码封装函数

发布时间:2022-08-22 22:04:06

Ⅰ 怎么把下面的python代码封装成函数

Python:常用函数封装:
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False

def is_number(uchar):
"""判断一个unicode是否是数字"""
if uchar >= u'\u0030' and uchar<=u'\u0039':
return True
else:
return False

def is_alphabet(uchar):
"""判断一个unicode是否是英文字母"""
if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):
return True
else:
return False

def is_other(uchar):
"""判断是否非汉字,数字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False

def B2Q(uchar):
"""半角转全角"""
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)

def Q2B(uchar):
"""全角转半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
return uchar
return unichr(inside_code)

def stringQ2B(ustring):
"""把字符串全角转半角"""
return "".join([Q2B(uchar) for uchar in ustring])

def uniform(ustring):
"""格式化字符串,完成全角转半角,大写转小写的工作"""
return stringQ2B(ustring).lower()

def string2List(ustring):
"""将ustring按照中文,字母,数字分开"""
retList=[]
utmp=[]
for uchar in ustring:
if is_other(uchar):
if len(utmp)==0:
continue
else:
retList.append("".join(utmp))
utmp=[]
else:
utmp.append(uchar)
if len(utmp)!=0:
retList.append("".join(utmp))
return retList

Ⅱ 如何用Python封装C语言的字符串处理函数

在C语言中,字符串处理是每天都要面对的问题。我们都知道C语言中其实并没有一种原生的字符串类型,‘字符串’在C语言里只是一种特殊的以''结尾的字符数组。因此,如何将C语言与更高层次的Python语言在‘字符串’处理这个问题上对接是一个有难度的问题。所幸有swig这种强大的工具。

如何封装一个函数,它修改参数字符串的内容

假如有这样一个C语言的函数,
<!-- lang: cpp -->
void FillZero(char* pc,size_t * piLen)
{
size_t i=0;
while(i++<*piLen/2 )
*pc++ = '0';
*pc = 0;
*piLen = i+1;
}

这个函数的功能是把字符串变成n个0。不过我们更关注函数的形式。这样的函数,表面上看char* pc是函数的参数,可是实际上它才是函数的返回值和执行的结果。piLen这个参数既是pc的最大长度,也是新的字符串的长度。我们直接用python封装,看看运行结果。

Type "help", "right", "credits" or "license" for more information.
>>> import cchar
>>> s='123456'
>>> cchar.FillZero(s,6)
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
TypeError: in method 'FillZero', argument 2 of type 'size_t *'

结果差强人意,不是我们想要得到的结果。函数的第二个参数为size_t* 我们很难用python来表示,而且python中也不存在既是输入,也是输出的参数。

swig有一个标准库,其中有一个cstring.i文件就是用来解决C语言字符串类型的问题。

我们在.i文件中加入这样几行
<!-- lang: cpp -->
%include "cstring.i"
%cstring_output_withsize(char* pc,size_t* pi)
void FillZero(char* pc, size_t* pi);

然后运行看结果

Type "help", "right", "credits" or "license" for more information.
>>> import cchar
>>> cchar.FillZero(10)
'00000\x00'
>>> s=cchar.FillZero(10)
>>> print s
00000

我们看函数的变化。首先在python里, FillZero变成了只有一个参数的函数。然后函数的返回值变成了一个字符串。其实cstring_output_size其实是一个宏,通过这个宏的定义改变了函数的形式,直接在Python中得到我们想要的结果。

其实类似cstring_output_size的宏还有好几个,我列举一下:

cstring_output_allocate(char *s,free($1));
第一个参数是指向字符串地址的指针,第二个参数为释放空间的方法。
大家考虑这一下这样的函数:
void foo(char* & s)
{

s = (char*)malloc(10);
memcpy(s,"123456789",9);

}

s这个参数表面上看是输入,实际上是函数真正的输出。 函数中真正改变的东西是char&s指向的字符串的值。而且char&这个类型,

python或者其他脚本语言里应该都没有对应的类型。那么我们用cstring_output_allocate将这个函数转换成另外一个形式的python或者其他脚本语言的函数。转换后的函数其实是这样的,以python为例str
foo()。
<!-- lang: cpp -->
%mole a
%include "cstring.i"
%{
void foo(char*& s);
%}
%cstring_output_allocate(char *&s, free(*$1));
void foo(char *&s);

在python中的调用:

<!-- lang: python -->
>>> import a
>>> a.foo()
'123456789'
>>>

cstring_output_maxsize(char *path, int maxpath);
第一个参数也是可以改变的字符串首地址,第二个参数为字符串的最大长度。在Python中调用的时候,只有maxpath这个参数,返回字符串。
cstring_output_allocate(char *s, free($1));
第一个参数为指向字符串首地址的指针,第二个参数为释放指针的方法。这个宏主要是封装一种直接在函数内部malloc空间的函数。在Python中调用时没有参数,直接返回字符串。
cstring_output_allocate_size(char *s, int slen, free(*$1));
这个相当于前面两个函数的组合。在函数内部malloc空间,然后将字符串长度通过slen返回。其实在调用的时候非常简单,没有参数,直接返回字符串。

如何处理c++的std::string

std::string是C++标准类库STL中常见的类。在平时工作中大家肯定是没少用。在python中如何封装std::string? swig提供了标准库

例如函数:
<!-- lang: cpp -->
string Repeat(const string& s)
{
return s+s;
}

只要在swig中加入这样几行:
<!-- lang: cpp -->
%include "std_string.i"
using namespace std;
string Repeat(const string& s);

运行结果:

Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "right", "credits" or "license" for more information.
>>> import cchar
>>> cchar.Repeat('123')
'123123'

使用起来很方便,但需要注意的是,假如函数的参数的内容是可以被修改,就不能用这种方式封装。
例如:
<!-- lang: cpp -->
void repeat(string s)
{
s+=s;
}

这样的函数直接使用 'std_string.i' 就是无效的。遇到这种函数,只能用C语言封装成 void repeat(chars, int maxsize), 再用swig调用 'cstring_output_withsize' 这个宏再封装一次了。

Ⅲ Python如何把一段代码封装起来重复使用

你可以用类或者函数将其封装一下,将需要后期变动的变量作为参数放在其中,可以使用默认值传参。

举个简单函数和类的栗子:

#函数封装
defsum(a,b=3):
returna+b
#类封装
classS(object):
def__init__(self,a=2,b=3)
self.a=a
self.b=b
defsum(self):
returnself.a+self.b

Ⅳ 将下面Python代码封装成函数

Python:常用函数封装:
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False

def is_number(uchar):
"""判断一个unicode是否是数字"""
if uchar >= u'\u0030' and uchar<=u'\u0039':
return True
else:
return False

def is_alphabet(uchar):
"""判断一个unicode是否是英文字母"""
if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):
return True
else:
return False

def is_other(uchar):
"""判断是否非汉字,数字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False

def B2Q(uchar):
"""半角转全角"""
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)

def Q2B(uchar):
"""全角转半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
return uchar
return unichr(inside_code)

def stringQ2B(ustring):
"""把字符串全角转半角"""
return "".join([Q2B(uchar) for uchar in ustring])

def uniform(ustring):
"""格式化字符串,完成全角转半角,大写转小写的工作"""
return stringQ2B(ustring).lower()

def string2List(ustring):
"""将ustring按照中文,字母,数字分开"""
retList=[]
utmp=[]
for uchar in ustring:
if is_other(uchar):
if len(utmp)==0:
continue
else:
retList.append("".join(utmp))
utmp=[]
else:
utmp.append(uchar)
if len(utmp)!=0:
retList.append("".join(utmp))
return retList

Ⅳ python如何封装函数

可以定义一个类,类里定义很多函数(主要用它做什么)或直接定义函数在一个py文件中
在另一个文件中导入这个那个py包,调用类和方法
就是封装了

Ⅵ python 函数如何封装

不是不可以,只是这样做没有意义,另外变量作用域的问题,会使得操作变得很复杂

Ⅶ python 如何封装在某带参数函数之后等待时间

题主你好,

方法及相应代码见截图:

*.方法不只一种, 题主看看如果不合适请追问. 上面这种做法的好处是封装的这个函数func可以带任意多个位置参数. //就图主的问题来看, *args就够了, 如果func函数中还有关键字参数,则还需要使用**argv.

-----

希望可以帮到题主, 欢迎追问

Ⅷ 求将下面的Python代码封装成函数,谢谢!!

Python:常用函数封装:
def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False

def is_number(uchar):
"""判断一个unicode是否是数字"""
if uchar >= u'\u0030' and uchar<=u'\u0039':
return True
else:
return False

def is_alphabet(uchar):
"""判断一个unicode是否是英文字母"""
if (uchar >= u'\u0041' and uchar<=u'\u005a') or (uchar >= u'\u0061' and uchar<=u'\u007a'):
return True
else:
return False

def is_other(uchar):
"""判断是否非汉字,数字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False

def B2Q(uchar):
"""半角转全角"""
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)

def Q2B(uchar):
"""全角转半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
return uchar
return unichr(inside_code)

def stringQ2B(ustring):
"""把字符串全角转半角"""
return "".join([Q2B(uchar) for uchar in ustring])

def uniform(ustring):
"""格式化字符串,完成全角转半角,大写转小写的工作"""
return stringQ2B(ustring).lower()

def string2List(ustring):
"""将ustring按照中文,字母,数字分开"""
retList=[]
utmp=[]
for uchar in ustring:
if is_other(uchar):
if len(utmp)==0:
continue
else:
retList.append("".join(utmp))
utmp=[]
else:
utmp.append(uchar)
if len(utmp)!=0:
retList.append("".join(utmp))
return retList

Ⅸ python中什么是封装

python中封装指的是把一些功能性的语句封装成一个函数供外面调用

阅读全文

与python代码封装函数相关的资料

热点内容
修改本地账户管理员文件夹 浏览:416
python爬虫工程师招聘 浏览:283
小鹏p7听音乐哪个app好 浏览:354
linux下的防火墙 浏览:954
凌达压缩机美芝压缩机 浏览:350
php后面代码不执行 浏览:236
微我手机怎样设置应用加密 浏览:202
条件加密 浏览:628
androidstudio设置中文 浏览:641
汽车换压缩机能提升制冷 浏览:628
安卓开发配什么电脑 浏览:607
linux下php模块 浏览:78
阿里云服务器终端在哪里 浏览:147
app纸有什么用 浏览:224
cuteftp命令 浏览:507
最开始的编程语言是什么 浏览:759
at远程命令 浏览:492
云服务器哪家好点 浏览:215
android系统源码阅读 浏览:931
dumpjava分析工具 浏览:680