[英]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.