我刚学 python,似乎犯了一个小错误。使用此代码,对 Patterns 的构造函数的使用会出现错误。
"cit_ref = 模式(patstring,3) 类型错误:此构造函数不带任何参数”
import re
class Patterns:
"""A simple struct to hold our regex """
def __init__(self, pat, bodynumArg=2):
self.pat = pat
self.bodynum = bodynumArg
self.reg = re.compile(self.pat, re.M+re.S)
patstring = r'<((us-)?references-cited)>(.*?)</\1>'
cit_ref = Patterns( patstring, 3)
最佳答案
您只需要缩进您的 __init__
函数,使其位于 class Patterns
下。像这样:
import re
class Patterns:
"""A simple struct to hold our regex """
def __init__(self, pat, bodynumArg=2):
self.pat = pat
self.bodynum = bodynumArg
self.reg = re.compile(self.pat, re.M+re.S)
patstring = r'<((us-)?references-cited)>(.*?)</\1>'
cit_ref = Patterns( patstring, 3)
在 Python 中,缩进与方括号/大括号在其他语言中的作用基本相同 - 它们分隔代码,因此您必须小心缩进。
关于python - 初学者的 python 类构造函数不带参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14866427/