繁体   English   中英

从 python2 调用 python3 方法

[英]calling python3 method from python2

我想从 python3 模块中的某些特定 class 调用 python3 方法,使用 python2 已使用进程调用,但我无法理解如何使用子进程创建 object 然后调用一些特定的 ZC1C425268E18385D1ABZF50444

python3代码:

from datetime import date

# random Person
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def fromBirthYear(cls, name, birthYear):
        return cls(name, date.today().year - birthYear)

    def display(self):
        print(self.name + "'s age is: " + str(self.age))

person = Person('Adam', 19)
person.display()

person1 = Person.fromBirthYear('John',  1985)
person1.display()

python2代码:

import subprocess
import sys
child = subprocess.Popen("python3 python3.py", stderr=subprocess.PIPE)
while True:
    out = child.stderr.read(1)
    if out == '' and child.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

我想从python2创建object本身

我将参数和 function 名称作为 arguments 传递,并从 cmd 行在 python3 中读取它们。

python 2 代码

child = subprocess.Popen("python3 f4t_control.py "+self.IP+" get_setpoint", stdout=subprocess.PIPE)
out = child.stdout.read()
return float(out)

python3代码

n=len(sys.argv)
x=F4TController(host=sys.argv[1],timeout=1)
if(n==3):
    if(sys.argv[2]=='get_setpoint'):
        print(format(x.get_setpoint()))
    else:
        x.set_temperature(float(sys.argv[2]))

我可以通过子进程从 cmd 行获取参数来创建 python3 class 的 object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM