如果我上课了......
class MyClass:
def method(arg):
print(arg)
Run Code Online (Sandbox Code Playgroud)
...我用来创建一个对象......
my_object = MyClass()
Run Code Online (Sandbox Code Playgroud)
......我就这样打电话method("foo")......
>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)
Run Code Online (Sandbox Code Playgroud)
...为什么Python告诉我我给了它两个参数,当我只给出一个?
我正在写一小段python作为家庭作业,我不会让它运行!我没有那么多的Python经验,但我知道很多Java.我正在尝试实现粒子群优化算法,这就是我所拥有的:
class Particle:
def __init__(self,domain,ID):
self.ID = ID
self.gbest = None
self.velocity = []
self.current = []
self.pbest = []
for x in range(len(domain)):
self.current.append(random.randint(domain[x][0],domain[x][1]))
self.velocity.append(random.randint(domain[x][0],domain[x][1]))
self.pbestx = self.current
def updateVelocity():
for x in range(0,len(self.velocity)):
self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x])
def updatePosition():
for x in range(0,len(self.current)):
self.current[x] = self.current[x] + self.velocity[x]
def updatePbest():
if costf(self.current) < costf(self.best):
self.best = self.current
def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
particles = []
for i in range(noOfParticles):
particle = Particle(domain,i)
particles.append(particle)
for i in range(noOfRuns): …Run Code Online (Sandbox Code Playgroud) class MyClass:
def say():
print("hello")
mc = MyClass()
mc.say()
Run Code Online (Sandbox Code Playgroud)
我收到了错误:TypeError: say() takes no arguments (1 given).我做错了什么?