【问题标题】:Python oops concept [closed]Python oops 概念
【发布时间】:2020-02-05 12:34:36
【问题描述】:

我有 2 个 python 程序,即 a1.py、a2.py

我想在 a2.py 中导入 a1.py。

以下是示例代码: a1.py:

class a:
    def __init__(self):
        #code
    def __str__(self):
        #code
    def f1(self):
        #code
    def f2(self,file):
        #code to read a file
          and call f1 to do operations
        #returns final text as str 
m = a()
m.f2("file")

a2.py

class b:
    def __init__(self):
        #code
    def __str__(self)
        #code
    def f3(self,text) #this text is the output of a1.py
        #code
    def f4(self)
        #code gives the final output as list and calls f3

n = b()
n.f4()

如何在 a2.py 中使用 a1.py 的输出?

【问题讨论】:

标签: python python-3.x oop


【解决方案1】:

你有吗

m = a()
m.f2("file")

在 a1.py 中?

使用 a2.py 可能会更容易:

from a1 import a

class b:
    def __init__(self):
        #code

    def __str__(self)
        #code

    def f3(self,text) #this text is the output of a1.py
        #code

    def f4(self)
        #code gives the final output as list and calls f3

n = b()
n.f4()
m = a()
m.f2("file")

【讨论】:

  • 嗨@ZhiYongLee,我想在f3中使用f2的最终输出。是的,必须在a1.py中保留m = a
猜你喜欢
  • 2020-02-05
  • 1970-01-01
  • 2011-04-23
  • 2018-09-13
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多