【问题标题】:Python : Result Ranking by classes?Python:按类别排名结果?
【发布时间】:2016-07-29 13:26:52
【问题描述】:

我将三个类分开如下:

file1.py

class one():
    def __init__(self):
        self.x = 1
        self.y = 3
        self.h = self.x + self.y

file2.py

from file1 import *
class two():
    def __init__(self,coming):
        self.get = coming.h+1

file3.py

from file1 import *
from file2 import *
class three():
    def __init__(self,f1,f2):
        self.v1 = f1.h
        self.v2 = f2.get
        print(self.v1)
        print(self.v2)

k = three(two(one()))

每当我应该运行 file3.py 时,我都需要这样的结果:

> 4
> 5

但是我的代码给出了 file2 没有返回任何值,那么这里是我的错误:

~#python file3.py

Traceback (most recent call last):
  File "file3.py", line 10, in <module>
    k = three(two(one()))
TypeError: __init__() takes exactly 3 arguments (2 given)

【问题讨论】:

  • 您的three 类希望您提供两个参数(第三个是自动传入的self)。

标签: python python-2.7 class python-3.x


【解决方案1】:

问题是三个得到 2 个参数而不是 3 个。 您可以将 file3 中的最后一行更改为:

k = three(one(), two(one()))

或以不同的方式设计代码

【讨论】:

  • 我尝试像你告诉我的那样订购课程,但我在某些实例的属性中遇到问题:当我使用 k = three(two(one()),one()) ==> Traceback (most recent call last): File "file3.py", line 10, in &lt;module&gt; k = three(two(one()),one()) File "file3.py", line 5, in __init__ self.v1 = f1.h AttributeError: two instance has no attribute 'h'时出现错误
  • 你能给我提供一个代码示例来展示这种类型的类值应该如何传递概念吗?
  • 嘿,假设我们只有一班,二班!!!经过第二类()的这种处理后,我想传递h 的值。每当我在 file1.py 中进行导入时,我都会在 file2.py 中导入它说NameError: name 'two' is not defined !!!所以当我试图让价值在二等治疗后回到一等时我面临问题!谁可能是代码!
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 2013-10-03
  • 2018-07-25
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多