GVKun编程网logo

在Python中将函数传递给re.sub(python把函数传入函数)

9

对于在Python中将函数传递给re.sub感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python把函数传入函数,并为您提供关于python–将参数传递给扭曲的工厂以传递给会话、Pytho

对于在Python中将函数传递给re.sub感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python把函数传入函数,并为您提供关于python – 将参数传递给扭曲的工厂以传递给会话、Python中将函数作为另一个函数的参数传入并调用、python中将函数导入到模块中、python多继承使用super将参数传递给构造函数的有用信息。

本文目录一览:

在Python中将函数传递给re.sub(python把函数传入函数)

在Python中将函数传递给re.sub(python把函数传入函数)

我的字符串中某些地方包含数字,并且我正尝试用其单词符号替换此数字(即3->
3)。我有一个功能可以做到这一点。现在的问题是找到字符串中的数字,同时保持字符串的其余部分不变。为此,我选择使用该re.sub函数,该函数可以接受“
callable”。但是,传递给它的对象是内部对象_sre.SRE_Match,我不确定如何处理它。我的函数接受数字或其字符串表示形式。

我应该如何编写一些辅助函数,该函数可用于将re.sub调用与执行所需处理的函数桥接起来?另外,是否有更好的方法来做我想做的事?

答案1

小编典典

您应该致电group()以获取匹配的字符串:

import renumber_mapping = {''1'': ''one'',                  ''2'': ''two'',                  ''3'': ''three''}s = "1 testing 2 3"print re.sub(r''\d'', lambda x: number_mapping[x.group()], s)

印刷品:

one testing two three

python – 将参数传递给扭曲的工厂以传递给会话

python – 将参数传递给扭曲的工厂以传递给会话

我已经编写了一个基于扭曲的sshsimpleserver.py的sshdaemon,效果很好.

http://twistedmatrix.com/documents/current/conch/examples/

但我现在想要将命令行参数传递给EchoProtocol,以根据参数改变它的行为.
我怎样才能做到这一点?在这种情况下,我想通过
‘options.test’参数到我的协议.

[...]

if __name__ == '__main__':
     parser = optparse.OptionParser()
     parser.add_option('-p','--port',action = 'store',type = 'int',dest = 'port',default = 1235,help = 'server port')
     parser.add_option('-t','--test',type = 
'string',dest = 'test',default = '123')
     (options,args) = parser.parse_args()

     components.registeradapter(ExampleSession,ExampleAvatar,session.ISession)

     [...]

     reactor.listenTCP(options.port,ExampleFactory())
     reactor.run()

由于会话实例是由工厂创建的,我似乎无法做到
能够将其他args传递给会话构造函数和协议.
我已经尝试将选项名称设置为全局,但它在协议上下文/范围中不可见.

顺便说一句.我将协议类移动到它自己的文件中并将其导入主文件中.

最佳答案
您可以创建自己的Factory并将参数传递给它.请参阅docs中的示例

from twisted.internet.protocol import Factory,Protocol
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

class QOTD(Protocol):

    def connectionMade(self):
        # self.factory was set by the factory's default buildProtocol:
        self.transport.write(self.factory.quote + '\r\n')
        self.transport.loseConnection()


class QOTDFactory(Factory):

    # This will be used by the default buildProtocol to create new protocols:
    protocol = QOTD

    def __init__(self,quote=None):
        self.quote = quote or 'An apple a day keeps the doctor away'

endpoint = TCP4ServerEndpoint(reactor,8007)
endpoint.listen(QOTDFactory("configurable quote"))
reactor.run()

Python中将函数作为另一个函数的参数传入并调用

Python中将函数作为另一个函数的参数传入并调用

Python中将函数作为另一个函数的参数传入并调用

将函数作为另外一个函数的参数调用?听起来很复杂,其实动起手来很简单。下面跟我一起看看一些简单的例子,我尽量简单易懂:

下面这段代码涵盖了函数调用的函数的传参和不传参的两个方面:

def func_a(func_a_arg_a, func, **kwargs):
    print(func_a_arg_a)
    func(**kwargs)


def func_b(arg_a):
    print(arg_a)


def func_c():
    print(''Hello World'')


if __name__ == ''__main__'':
    func_a(func_a_arg_a=''temp'', arg_a=''Hello Python'', func=func_b)
    func_a(func_a_arg_a=''temp'', func=func_c)

输出结果:
temp
Hello Python
temp
Hello World
[Finished in 0.2s]

**注意:对于func_a,主函数,传参随意了,需要什么穿什么,但是对于func_b and func_c就不尽然了;

  • func_c作为不需要传参的函数,如果在func_a中有参数传入了会怎么样呢?

------当时然报错啊,可以自动动手尝试下,毕竟敲代码的人动手才是正途:


TypeError: func_c() got an unexpected keyword argument TypeE ''arg_a''
  • 对于fun_b:需要传参的函数:

要注意的是:不传参报错:
TypeError: func_b() missing 1 required positional argument: ''arg_a''

不懂就百度,多参考一些blog在动手,参考是关键,动手更关键!!!

python中将函数导入到模块中

python中将函数导入到模块中

 

import语句允许在当前运行的程序文件中使用模块中的代码。

要让函数是可导入的,得先创建模块。模块是扩展名为.py的文件。

1、创建module1,命名module1.py,定义三个函数fun1、fun2、fun3

def fun1(x):
print("11111",x)


def fun2(x):
print("22222",x.title())


def fun3(x):
print("33333",x.upper())

 

>>> import module1     ## import语句 导入module1模块
>>> module1.fun1("aaa")   ## 调用fun1
11111 aaa
>>> module1.fun2("aaa")    ## 调用fun2
22222 Aaa
>>> module1.fun3("aaa")    ## 调用fun3
33333 AAA

 

python多继承使用super将参数传递给构造函数

python多继承使用super将参数传递给构造函数

考虑以下python代码段

class A(object):
    def __init__(self,a):
        self.a = a

class B(A):
    def __init__(self,a,b):
        super(B,self).__init__(a)
        self.b = b

class C(A):
    def __init__(self,c):
        super(C,self).__init__(a)
        self.c = c

class D(B,C):
    def __init__(self,b,c,d):
        #super(D,self).__init__(a,c) ???
        self.d = d

我想知道我是如何能够及格abc以相应的基类的构造函数。

今天关于在Python中将函数传递给re.subpython把函数传入函数的讲解已经结束,谢谢您的阅读,如果想了解更多关于python – 将参数传递给扭曲的工厂以传递给会话、Python中将函数作为另一个函数的参数传入并调用、python中将函数导入到模块中、python多继承使用super将参数传递给构造函数的相关知识,请在本站搜索。

本文标签: