小编Dav*_*avó的帖子

Python:字符串替换索引

我的意思是,我想替换str[9:11]另一个字符串.如果我这样做str.replace(str[9:11], "###")它不起作用,因为序列[9:11]可以不止一次.如果str是"cdabcjkewabcef"我会得到"cd###jkew###ef"但我只想替换第二个.

python string python-3.x

7
推荐指数
1
解决办法
2万
查看次数

Python @cls.decorator

我想创建一个 python 装饰器,将某个函数添加到该类的函数列表中,这些函数有时会被处理。示例代码:

class A:

  # every subclass should define _list

  @classmethod
  def decorator(cls, f):
    # cls = B
    cls._flist.append(f)
    return f

  @classmethod
  def processFunctions(cls):
    for f in cls._flist:
      ...

class B(A):
  _flist = []

  @B.decorator # Obviously not possible because cls is not defined (yet)
  def foo(self):
    print("Inside foo")
Run Code Online (Sandbox Code Playgroud)

是否可以复制这种行为?装饰函数时应传递类 (cls),因此我无法使用创建“解包”cls 和其他参数的包装函数的常用方法。

python python-decorators

0
推荐指数
1
解决办法
470
查看次数

标签 统计

python ×2

python-3.x ×1

python-decorators ×1

string ×1