相关疑难解决方法(0)

TypeError:method()占用1个位置参数,但给出了2个

如果我上课了......

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 methods arguments self python-3.x

210
推荐指数
8
解决办法
41万
查看次数

为什么python对某些东西使用两个下划线?

我对实际的编程语言很新,而Python是我的第一个.我知道我在Linux上的方式,足以获得暑期工作(我还在上高中),在工作中,我有很多空闲时间,我正在学习Python.

有一件事让我感到高兴.当你有像这样的表达式时,Python究竟有什么不同

x.__add__(y) <==> x+y
x.__getattribute__('foo') <==> x.foo
Run Code Online (Sandbox Code Playgroud)

我知道什么方法做什么和做什么,我得到他们做的,但我的问题是:上面的双下划线方法与它们更简单的等价方法有何不同?

PS,我不介意在编程历史上讲课,事实上,我发现知道它非常有用:)如果这些主要是Python的历史方面,请随意开始漫步.

python double-underscore

51
推荐指数
4
解决办法
2万
查看次数

为什么我的类没有通过“def __int__”或“def _init_”初始化?为什么我会收到“不带参数”类型错误或属性错误?

如果您的问题作为此问题的重复项而被关闭,那是因为您有一个代码示例,其中包含以下任一内容:

class Example:
    def __int__(self, parameter):
        self.attribute = parameter
Run Code Online (Sandbox Code Playgroud)

或者:

class Example:
    def _init_(self, parameter):
        self.attribute = parameter
Run Code Online (Sandbox Code Playgroud)

当您随后尝试创建该类的实例时,会发生错误:

>>> Example("an argument")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Example() takes no arguments
Run Code Online (Sandbox Code Playgroud)

(在某些版本的 Python 中,错误可能会显示TypeError: object.__new__() takes no parameters。)

或者,该类的实例似乎缺少属性:

>>> class Example:
...     def __int__(self): # or _init_
...         self.attribute = 'value'

>>> Example().attribute
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Example' object has no …
Run Code Online (Sandbox Code Playgroud)

python initialization class typeerror magic-methods

11
推荐指数
1
解决办法
5733
查看次数

Python:object .__ new __()不带参数

现在我正在开发一个程序,允许人们进行测试,将它们保存到数据库,然后打印它们.我一直收到错误:

Traceback (most recent call last):
File "C:/Users/Shepard/Desktop/Gradebook.py", line 50, in <module>
qs = QuestionStorage("questions.db")
TypeError: object.__new__() takes no parameters
Run Code Online (Sandbox Code Playgroud)

任何人都有任何想法?我假设它在QuestionStorage类的某个地方,但我不能完全解决任何问题.这是我第一次使用SQLite3,而且我遇到了很多麻烦,如果有人可以帮助我,那就太棒了.:)

import sqlite3
class QuestionStorage(object):
    def _init_(self, path):
        self.connection = sqlite3.connect(path)
        self.cursor = self.connection.cursor()

    def Close(self):
        self.cursor.close()
        self.connection.close()

    def CreateDb(self):
        query = """CREATE TABLE questions
                 (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""
        self.cursor.exeute(query)
        self.connection.commit()
        #self.cursor.close()

    def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4):
        self.cursor.execute("""INSERT INTO questions
                                VALUES (?, ?, ?, ?, ?, …
Run Code Online (Sandbox Code Playgroud)

python

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