新人求帮助:python中如何获取函数参数表

luochen1990 2011-12-18 04:37:25
现在问题是这样的,由于python允许将函数作为参数和返回值,我就写了一个append_func函数来测试,它传入f和c,其中f是某个作为参数的函数,c是一个字符串,然后该函数返回一个函数,这个函数执行f之后打印出字符串c。代码如下:

def show(x) :
print(x)

def append_func(f , c) :
def r(x) :
f(x)
print(c)
return r

print2 = append_func(show , "aa")
print2("hello , world !")

input("pause")

问题是现在show函数是单参函数,所以能传入append_func作为参数f ,如果我希望 append_func 对任意参数的函数f都可用(逻辑上是没有问题的,因为append_func函数的含义确实跟f的参数个数及类型无关), 应该如何修改呢?

所以我想如果能获取函数的参数表,就可以将append_func中定义的r定义为相同类型的函数,从而解决问题。

不知道如何获取函数参数表,或者如果有别的解决办法也请高手分享!!
...全文
1306 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
InOner 2011-12-27
  • 打赏
  • 举报
回复
inspect module...
InOner 2011-12-27
  • 打赏
  • 举报
回复
inspect module...
askandstudy 2011-12-20
  • 打赏
  • 举报
回复
如果只是实现10楼这样的效果,我觉得装饰器是可以实现的,装饰器可以带参数和不带参数的,网上应该也有很多文章。只是对这个还不是太熟悉,要写出楼主要的代码多少总得花点时间再看几篇文章。今天学习多线程的东西,不玩这个。楼主说装饰器是语法糖衣,就算是吧,呵呵
I_NBFA 2011-12-20
  • 打赏
  • 举报
回复
LZ还是说说需求吧,有必须这样吗
hhulx 2011-12-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 luochen1990 的回复:]

引用 8 楼 iambic 的回复:
已经说过两次让你自己看了。自己看个文档就能解决的问题不要浪费别人的时间。要是这个人连看文档或者google的能力都没有,这个人也不值得别人浪费时间。

不在这个帖子浪费时间了。


额,我只是觉得短短几行代码浪费不了大牛几分钟吧? 您已经回了3次帖了,为什么不为我这个笨蛋再多浪费一分钟把代码贴出来呢?

9楼的方法是可行的,确实这个问题只需要……
[/Quote]

感觉不需要,在调用 append_func 时并不调用其中 r,在后面调用 print1(print2)时才调用,这时候抛出异常没有必要。
angel_su 2011-12-20
  • 打赏
  • 举报
回复
不晓得你要干嘛,如果装饰器不合你用的话,那看看文档有关inspect.getargspec(func)
luochen1990 2011-12-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 iambic 的回复:]
已经说过两次让你自己看了。自己看个文档就能解决的问题不要浪费别人的时间。要是这个人连看文档或者google的能力都没有,这个人也不值得别人浪费时间。

不在这个帖子浪费时间了。
[/Quote]

额,我只是觉得短短几行代码浪费不了大牛几分钟吧? 您已经回了3次帖了,为什么不为我这个笨蛋再多浪费一分钟把代码贴出来呢?

9楼的方法是可行的,确实这个问题只需要理解以tuple的形式传递参数是怎么回事就可以了,而跟装饰器无关(装饰器只是语法糖衣,我这个例子确实解释了装饰器是干什么的,但装饰器解决不了我的问题)。

问题已经解决,代码很简单,我再把9楼的重新发一下:


def show1(x) :
print(x)

def show2(x , y) :
print(x)
print(y)

def append_func(f , c) :
def r(*x) :
f(*x)
print(c)
return r

print1 = append_func(show1 , "aa")
print1("hello , world !")
print2 = append_func(show2 , "aa")
print2("hello " , " world !")

input("pause")


只是我又在这个实现中发现了一个新问题:形参r在接受参数*x之后并没有进行类型检查,而是在将*x传给f的时候检查的,确实这很容易理解:因为这样定义的r并没有保证参数表和f是一样的,也就是说我们最后返回的函数不保证参数和f一样,只是它调用f的时候发现无法匹配才报错,个人觉得这个有点不完美,不知道还有没有别的解决方案?
askandstudy 2011-12-20
  • 打赏
  • 举报
回复
不好意思,一下疏忽,上面复制的代码错了,重新贴,免得被人认为造假:


#下面是用装饰器来实现的
print '='*20,'use decorator','='*20
def decorator1(arg1):
def mydeco(f):
def wrapper(*args,**kwargs):
try:
return f(*args,**kwargs)
finally:
print arg1

return wrapper

return mydeco

@decorator1("bb")
def show3(x) :
print(x)

@decorator1("bb")
def show4(x , y) :
print(x)
print(y)

show3("hello , world !")
show4("hello " , " world !")


google到的链接里正好有篇文章是有关调用参数合法性的,你自己去看吧:
Python装饰器实例:调用参数合法性验证


如果我写的例子看起来比较丑陋,把装饰器运用得这么差劲,那是因为我是菜鸟的原因,用python的时间也较短,抱歉

askandstudy 2011-12-20
  • 打赏
  • 举报
回复
你要10楼这种结果不是很简单吗,直接从前几天回帖的代码中改造一下,再加上你的代码对比一下,如下:
其实楼主也只是想快速的得到想要的答案,毕竟谁去研究这个问题都是需要花时间的,理解



#!/usr/bin/env python
#coding=utf-8

print '='*20,'your code','='*20
def show1(x) :
print(x)

def show2(x , y) :
print(x)
print(y)

def append_func(f , c) :
def r(*x) :
f(*x)
print(c)
return r

print1 = append_func(show1 , "aa")
print1("hello , world !")
print2 = append_func(show2 , "aa")
print2("hello " , " world !")

#input("pause")
#下面是用装饰器来实现的
print '='*20,'use decorator','='*20
def decorator1(arg1):
def mydeco(f):
def wrapper(*args,**kwargs):
try:
return f(*args,**kwargs)
finally:
print "aa"

return wrapper

return mydeco

@decorator1("aa")
def show3(x) :
print(x)

@decorator1("aa")
def show4(x , y) :
print(x)
print(y)

show3("hello , world !")
show4("hello " , " world !")





运行结果如下,你看是不是跟你的一样:


E:\codes\komodoprj>c:\python27\python.exe temp2.py
==================== your code ====================
hello , world !
aa
hello
world !
aa
==================== use decorator ====================
hello , world !
aa
hello
world !
aa

E:\codes\komodoprj>
hhulx 2011-12-19
  • 打赏
  • 举报
回复
这样就可以了

[Quote=引用 6 楼 luochen1990 的回复:]

把我的代码重新发一下:
Python code


def show(x) :
print(x)

def append_func(f , c) :
def r(*x) :
f(*x)
print(c)
return r

print2 = append_func(show , "aa")
print2("hello , world !")……
[/Quote]
iambic 2011-12-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 luochen1990 的回复:]
iambic对吧,请写出你的实现,谢谢!
[/Quote]

已经说过两次让你自己看了。自己看个文档就能解决的问题不要浪费别人的时间。要是这个人连看文档或者google的能力都没有,这个人也不值得别人浪费时间。

不在这个帖子浪费时间了。
askandstudy 2011-12-18
  • 打赏
  • 举报
回复
没错的,google:python 装饰器
这东西可以实现你的要求
luochen1990 2011-12-18
  • 打赏
  • 举报
回复
把我的代码重新发一下:


def show(x) :
print(x)

def append_func(f , c) :
def r(x) :
f(x)
print(c)
return r

print2 = append_func(show , "aa")
print2("hello , world !")

input("pause")


luochen1990 2011-12-18
  • 打赏
  • 举报
回复
iambic对吧,请写出你的实现,谢谢!
I_NBFA 2011-12-18
  • 打赏
  • 举报
回复
这个问题好像前些日子有人问过的那个.
iambic 2011-12-18
  • 打赏
  • 举报
回复
你对1楼的理解有误。让你看python decorator的tutorial就去看。
luochen1990 2011-12-18
  • 打赏
  • 举报
回复
1楼理解有误,我并非要定义一个参数数目可变的函数,而是要使append_func对任意函数都可用。

变长参数列表的方法只能通过修改传入的函数(如例中的show)的定义,使其在append_func中可用,但是对append_func的使用仍然是受限制的,而我要是的append_func对任意函数都可以使用!
iambic 2011-12-18
  • 打赏
  • 举报
回复
变长参数列表。
你看下python decorator的tutorial吧。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧