如何将局部变量导入 python 中的另一个文件?

How do i import local variable to the another file in python?

从 A.py 到 B.py 文件

A.py 文件

def ticket():
    .
    .
    .
    tickets = child + student + senior + adult
    total_price = child * Child_price + student * student_price + senior * senior_price + adult * adult_price

如何将 total_price 从 A.py 文件导入到 B.py 文件

查看以下解决方案:

您需要在文件 B 中导入文件 A 并且 return 来自文件 B

的值

文件 A.py 包含:

def ticket():
    .
    .
    .
    .
    tickets = child + student + senior + adult
    total_price = child * Child_price + student * student_price + senior * senior_price + adult * adult_price
    return(tickets,total_price)

文件内容 B.py:

from A import ticket
tickets,total_price= ticket()
print(tickets,total_price)

import A
print(A.ticket())

from A import ticket
print(ticket())