在查找一些pyQt示例时,我遇到了folloqing类型的代码:
class DisplayPage(QWizardPage):
def __init__(self, *args):
apply(QWizardPage.__init__, (self, ) + args)
Run Code Online (Sandbox Code Playgroud)
*args是什么意思?
使用此类代码申请的目的是什么?
我知道这是有效的:
def printValue():
print 'This is the printValue() method'
def callPrintValue(methodName):
methodName()
print 'This is the callPrintValue() method'
Run Code Online (Sandbox Code Playgroud)
但有没有办法传递一个接收参数作为另一个函数的参数的方法?
这样做是不可能的:
def printValue(value):
print 'This is the printValue() method. The value is %s'%(value)
def callPrintValue(methodName):
methodName()
print 'This is the callPrintValue() method'
Run Code Online (Sandbox Code Playgroud)
这是我得到的堆栈跟踪:
This is the printValue() method. The value is dsdsd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in callPrintValue
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud) 首先,我确实意识到这是一个非常简单的问题,请耐心等待.
如何在python中获取字符串的数量?我想做这样的事情:
def func(input_strings):
# Make the input_strings iterable
if len(input_strings) == 1:
input_strings = (input_strings, )
# Do something
for input_string in input_strings:
analyze_string(input_string)
return
Run Code Online (Sandbox Code Playgroud)
所以使用这个函数,如果输入是一个列表,['string1','string2','string3'],它将循环遍历它们; 如果输入只是一个字符串,如'string1',那么它仍然会处理它,而不是抛出异常.
但是,len(str)返回字符串中的字符数,并且不会给我"1".
我真的很感谢你的帮助!
Python中打印函数中"*"的作用是什么?
print ("Hello World!\n")
print (*"Hello World!\n")
Run Code Online (Sandbox Code Playgroud)
第一个打印功能的输出是
Hello World!
Run Code Online (Sandbox Code Playgroud)
第二个功能的输出是
H e l l o W o r l d !
Run Code Online (Sandbox Code Playgroud)
但是在python 2.7中它不起作用!