相关疑难解决方法(0)

@classmethod和@staticmethod对初学者的意义?

有人可以向我解释@classmethod@staticmethodpython中的含义吗?我需要知道差异和意义.

据我所知,@classmethod告诉一个类,它是一个应该继承到子类的方法,或者......某种东西.但是,重点是什么?为什么不在不添加@classmethod或定义@staticmethod任何@定义的情况下定义类方法?

tl; dr: 我应该何时使用它们,为什么要使用它们,我应该如何使用它们?

我在C++方面非常先进,所以使用更高级的编程概念应该不是问题.如果可能的话,请随意给我一个相应的C++示例.

python oop static-methods class-method

1532
推荐指数
7
解决办法
59万
查看次数

自我的目的是什么?

selfPython 中这个词的目的是什么?我理解它指的是从该类创建的特定对象,但我不明白为什么它明确需要作为参数添加到每个函数.为了说明,在Ruby中我可以这样做:

class myClass
    def myFunc(name)
        @name = name
    end
end
Run Code Online (Sandbox Code Playgroud)

我很容易理解.但是在Python中我需要包括self:

class myClass:
    def myFunc(self, name):
        self.name = name
Run Code Online (Sandbox Code Playgroud)

谁能跟我说说这个?这不是我在(无可否认的有限)经历中遇到的事情.

python oop class self

1061
推荐指数
17
解决办法
81万
查看次数

星级算子是什么意思?

*运算符在Python 中的含义是什么,例如在代码中zip(*x)f(**k)

  1. 如何在解释器内部处理?
  2. 它会影响性能吗?是快还是慢?
  3. 什么时候有用,什么时候不用?
  4. 它应该用于功能声明还是通话中?

python syntax parameter-passing argument-unpacking iterable-unpacking

559
推荐指数
5
解决办法
17万
查看次数

Python解释器错误,x不带参数(给定1个)

我正在写一小段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)

python methods arguments object

60
推荐指数
3
解决办法
9万
查看次数

TypeError:worker()获取0个位置参数但给出了1

我正在尝试实现一个子类,它会抛出错误:

TypeError: worker() takes 0 positional arguments but 1 was given

class KeyStatisticCollection(DataDownloadUtilities.DataDownloadCollection):
    def GenerateAddressStrings(self):
        pass    
    def worker():
        pass
    def DownloadProc(self):
        pass
Run Code Online (Sandbox Code Playgroud)

python python-3.x

58
推荐指数
5
解决办法
14万
查看次数

"_这个构造函数没有参数"__init__中的错误

我在运行以下代码时遇到错误:

class Person:
  def _init_(self, name):
    self.name = name

  def hello(self):
    print 'Initialising the object with its name ', self.name

p = Person('Constructor')
p.hello()
Run Code Online (Sandbox Code Playgroud)

输出是:

Traceback (most recent call last):  
  File "./class_init.py", line 11, in <module>  
    p = Person('Harry')  
TypeError: this constructor takes no arguments
Run Code Online (Sandbox Code Playgroud)

有什么问题?

python python-3.x

33
推荐指数
2
解决办法
5万
查看次数

"<method>不带参数(给出1个)"但我没有给出任何参数

我是Python的新手,我编写了这个简单的脚本:

#!/usr/bin/python3
import sys

class Hello:
    def printHello():
        print('Hello!')

def main():
    helloObject = Hello()
    helloObject.printHello()   # Here is the error

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

当我运行它(./hello.py)时,我收到以下错误消息:

Traceback (most recent call last):
  File "./hello.py", line 13, in <module>
    main()
  File "./hello.py", line 10, in main
    helloObject.printHello()
TypeError: printHello() takes no arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

为什么Python认为我给出了printHello()一个参数,而我显然没有?我做错了什么?

python methods call

24
推荐指数
3
解决办法
2万
查看次数

我收到类型错误。我如何解决它?

我经常从我的 Python 代码中得到未捕获的异常(错误),这些异常被描述为TypeErrors. 经过大量的实验和研究,我收集了以下示例(以及细微的变化):

TypeError: func() takes 0 positional arguments but 1 was given
TypeError: func() takes from 1 to 2 positional arguments but 3 were given
TypeError: func() got an unexpected keyword argument 'arg'
TypeError: func() missing 1 required positional argument: 'arg'
TypeError: func() missing 1 required keyword-only argument: 'arg'
TypeError: func() got multiple values for argument 'arg'
TypeError: MyClass() takes no arguments
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: can only concatenate str …
Run Code Online (Sandbox Code Playgroud)

python debugging typeerror

11
推荐指数
2
解决办法
8049
查看次数

seaborn FutureWarning:将以下变量作为关键字参数传递:x, y

我想绘制一个seaborn regplot。我的代码:

x=data['Healthy life expectancy']
y=data['max_dead']
sns.regplot(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud)

然而,这给了我未来的警告错误。如何修复此警告?

FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid 
positional argument will be 'data', and passing other arguments without an explicit keyword will 
result in an error or misinterpretation.
Run Code Online (Sandbox Code Playgroud)

python plot seaborn

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

builtins.TypeError: _findCaller() 需要 1 到 2 个位置参数,但 django 中给出了 3 个

我有一个 django 的抓取项目。一切正常,但终端显示此错误:

builtins.TypeError:_findCaller() 接受 1 到 2 个位置参数,但给出了 3 个

这是什么错误?

我该如何处理?

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/twisted/python/log.py", line 134, in err
    msg(failure=_stuff, why=_why, isError=1, **kw)
  File "/usr/lib/python3/dist-packages/twisted/python/threadable.py", line 53, in sync
    return function(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/twisted/python/log.py", line 286, in msg
    _publishNew(self._publishPublisher, actualEventDict, textFromEventDict)
  File "/usr/lib/python3/dist-packages/twisted/logger/_legacy.py", line 154, in publishToNewObserver
    observer(eventDict)
--- <exception caught here> ---
  File "/usr/lib/python3/dist-packages/twisted/logger/_observer.py", line 131, in __call__
    observer(event)
  File "/usr/lib/python3/dist-packages/twisted/logger/_legacy.py", line 93, in __call__
    self.legacyObserver(event)
  File "/usr/lib/python3/dist-packages/twisted/python/log.py", line 595, in emit
    _publishNew(self._newObserver, …
Run Code Online (Sandbox Code Playgroud)

python django

6
推荐指数
1
解决办法
4739
查看次数