python 类初始化和参数[重复]

pkwftd7m  于 2023-09-29  发布在  Python
关注(0)|答案(2)|浏览(94)

此问题已在此处有答案

"TypeError: method() takes 1 positional argument but 2 were given" but I only passed one(12个回答)
昨天关门了。
我知道这是超级基本的Python东西,但我没有想到这个概念。我错过了在__init__()下示例化对象的基本原因和结构
这是一个基本的例子,我不明白为什么要把self.tangerine="..."放在那里,为什么如果我添加self.order="order",即使这个参数没有添加到__init__(self, order)中,一切都正常工作

class MyStuff(object):

    def __init__(self):
        self.tangerine="And now a thousand years between"

    def apple(self):
        print "I AM CLASSY APPLE!" 

thing=MyStuff()
thing.apple()

print thing.tangerine

为了深入研究这个简单的例子,我在init中添加了一个变量:

class MyStuff(object):

    def __init__(self, order):
        self.tangerine="And now a thousand years between"
        self.order="order"

    def apple(self):
        print "I AM CLASSY APPLE!" 

thing=MyStuff()
thing.apple()

print thing.tangerine

现在我得到一个错误:

Traceback (most recent call last):
  File "ex40_a.py", line 11, in <module>
    thing=MyStuff()
TypeError: __init__() takes exactly 2 arguments (1 given)

在我看来,这里有两个论点(橘子(自我)和秩序)。有人能帮帮我吗?

scyqe7ek

scyqe7ek1#

第二段代码的剖析:

# Define class named MyStuff which inherits from object
class MyStuff(object):

    # Define initializer method for class MyStuff
    # This method accepts 2 arguments: self and order
    # self will hold newly created instance of MyStuff
    def __init__(self, order):
        # Assign a string value to field tangerine of current instance
        self.tangerine="And now a thousand years between"
        # Assign a string value to field order of current instance
        self.order="order"
        # Note that second argument (order) was not used

    # Define apple method for class MyStuff
    # This method accepts 1 argument: self
    # self will hold the instance of MyStuff
    def apple(self):
        # Print a string to standard output
        print "I AM CLASSY APPLE!" 

# Create instance of MyStuff
# Initializer is called implicitly and self is set to new instance
# Second argument (order) is missing, so you get exception
thing=MyStuff()
# Correct invocation would be
thing = MyStuff("some string value")
# Call method apple of MyStuff instance - statement correct but won't be reached
# due to former exception
thing.apple()

# Print value of field tangerine of MyStuff instance to standard output - again
# statement correct but won't be reached due to former exception
print thing.tangerine
  • 要阅读的内容:
c2e8gylq

c2e8gylq2#

看起来不错,但我假设您希望将订单值输入到对象中。另外,通常您不希望在类上使用print语句,而是返回它们,然后在需要时在代码中的其他位置打印它们

class MyStuff(object):
    def __init__(self, order):
        self.tangerine = "And now a thousand years between"
        self.order = order
    
    def apple(self):
        return "I AM CLASSY APPLE!" 

thing = MyStuff("I like strings and integer values")

print thing.order
print thing.tangerine
print thing.apple()

输出量:

I like strings and integer values 
And now a thousand years between 
I AM CLASSY APPLE!

你指定你想用这个调用你的类的参数:

def __init__(self, order):
    self.order = order

如果你不想用任何东西调用你的类,而只是使用字符串值,那么这样做:

def __init__(self):
    self.order = "order"

相关问题