Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在没有函数的情况下重复字符串_Python_String_Function - Fatal编程技术网

Python 如何在没有函数的情况下重复字符串

Python 如何在没有函数的情况下重复字符串,python,string,function,Python,String,Function,我发现了一个任务,其中要求我实现某个对象,该对象将字符串重复n次,但条件是该对象不能是函数。我尝试了以下方法: class str_repeat: def __init__(self,x,y): self.s = x * y def __repr__(self): return self.s 但是这不起作用。一些提示会有很大帮助stru repeat必须是可调用的 您认为: str_re

我发现了一个任务,其中要求我实现某个对象,该对象将字符串重复n次,但条件是该对象不能是函数。我尝试了以下方法:

class str_repeat:
         def __init__(self,x,y):
                  self.s = x * y
         def __repr__(self):
                  return self.s

但是这不起作用。一些提示会有很大帮助
stru repeat
必须是可调用的

您认为:

str_repeat = str.__mul__

你说的“这不起作用”是什么意思?你的课对我有用
print stru_repeat(“s”,4)
out:
ssss
什么是“不能是函数”?因为
\uuuu init\uuuu()
是一个函数,从技术上讲,
*
操作符也是一个函数。因此,除非该任务给出更多关于什么是允许的,什么是不允许的,并且只是说“你不能使用任何函数”,否则这是不可能的。虽然我假设编写该任务的人可能只是想让您使用
*
运算符。但输出很奇怪-“aaaa”应该等于“aaaa”。如果您想在您的案例中查看所有工作的描述,我将给您该任务的链接。只能创建对象和pront
s
my_string=str_repeat('string',5)
如果你这样做
print(my_string.s)
你会看到单词
string
5次哈,成功了,谢谢,但我有点失望,因为我想自己做,我只是想得到一个提示。无论如何,非常感谢