文件之间的Python全局变量

时间:2021-05-22 16:10:13

标签: python python-3.x

我有一个包含所有函数的 functions.py 文件。我想从 main.py 文件中打印一个变量,该文件是在 function.py 文件中的函数中创建的。我该怎么做?

functions.py:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()

def open_tradeview():
    browser.get('https://www.tradingview.com/chart/')
    wait = WebDriverWait(browser, 10)

    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[3]/div/div/div/div"))).click() #Click on Hamburger menu
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/span/div[1]/div/div/div[11]"))).click() #Click on sign in button
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span"))).click() #Click on sign in with email button
    wait.until(EC.visibility_of_element_located((By.NAME, "username"))).send_keys("trading_view_bot@fastmail.com") #type in email
    wait.until(EC.visibility_of_element_located((By.NAME, "password"))).send_keys("2C3-cc9r" + Keys.RETURN) #type in password
    time.sleep(18)
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[1]/div[3]/div/div/div/article/button"))).click() #Click ad away
    time.sleep(26)
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[1]/div[3]/div/div/div/div/article/button"))).click() #Click ad away
    time.sleep(5)
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div[2]/div/div[2]/div/div/div[1]/button"))).click() #Click ad away
    wait.until(EC.element_to_be_clickable((By.ID, "header-toolbar-symbol-search"))).click() #Currency pair
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-Hsmn_0WX.upperCase-Hsmn_0WX.input-3n5_2-hI"))).send_keys("VETUSD") #type in currency pair
    wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'VeChain / US Dollar (calculated by TradingView)')]"))).click() #select new currency pair
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[2]/div/div"))).click() #click on time frame
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/span/div[1]/div/div/div/div[12]/div"))).click() #select 30min time frame
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[5]/div/div"))).click() #click on indicators
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[1]/div[1]/div/div/div/div/div[5]/div/div"))).click() #click on indicators
    wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[2]/div/input"))).send_keys("ATR Trailing Stoploss") #search for ATR indicator
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[3]/div[2]/div/div/div[2]/div[1]"))).click() #click on ATR indicator
    wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[6]/div/div/div[1]/div/div[1]/span"))).click() #close search window

    
def get_atr_price():
    global atr
    global price
    atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
    price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
    

main.py:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
import yagmail
from functions import *
import functions
functions.get_atr_price()

open_tradeview()


get_atr_price()

print(atr)

当我想打印变量 atr 时,我收到一个错误,指出变量 atr 未定义

那么基本上我怎样才能访问两个文件中的每个变量?

5 个答案:

答案 0 :(得分:2)

你有没有尝试过这样的事情?

print(functions.atr)

这是一个对我有用的例子:

main.py:

import functions

print("hello from main!")
print(functions.hello)
functions.funky() # this changes the value
print(functions.hello)

functions.py:

hello = 4 # here the global variable is declared

def funky():
    print("hello from functions.funky()")
    global hello
    hello = 10 # changes the value

输出:

hello from main
4
hello from functions.funky()
10

编辑: 如果你想在没有全局变量的情况下工作,你可以让 get_atr_price() 函数返回 atr 和 price,如下所示:

functions.py:

def get_atr_price():
    atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
    price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
    return atr, price

然后,在您的 main.py 文件中,您可以像这样使用它们:

atr, price = functions.get_atr_price();

答案 1 :(得分:0)

在你的主函数中,你可以在一个变量中获取任何函数的结果然后打印

trade = open_tradeview()
atr = get_atr_price()

print(trade)
print(atr)

答案 2 :(得分:0)

虽然有时需要全局变量,但它们不应该是函数之间传递数据的正常方式。在您的情况下,为什么不直接从函数中返回您想要的值?

def get_atr_price():
    atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
    price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
    return atr, price

用法是

atr, price = get_atr_price()
print(atr)

答案 3 :(得分:0)

至少据我所知,全局不能跨文件工作,所以我通常在 python 中使用 return 命令,如果你想一次返回更多的东西,你可以每次 def 调用返回 1 个东西您需要制作一个列表/字典,然后将其返回。您还需要定义返回的变量/列表/字典将与之关联的变量,例如

functions.py 的变体 1:

def get_atr_price():
    atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
    price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
    return atr, price

main.py:

import functions

x = functions.get_atr_price()

现在 x 将是一个列表,其中 x[0] 将是 atr,x[1] 将是价格,或者您可以使用 dict 使其更好一点:

functions.py 的变体 2

def get_atr_price():
    atr = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div[1]/div').text
    price = browser.find_element_by_xpath('/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[1]/td[2]/div/div[1]/div[1]/div[2]/div[3]/span[2]').text
    tmp = {}
    tmp['atr'] = atr
    tmp['price'] = price
    return tmp

现在您几乎可以使用相同的代码直接调用 atr 或 price

main.py

import functions

x = functions.get_atr_price()

现在使用第二个变体,您可以将 x['art'] 称为艺术变量或 x['price'] ,它会更具扩展性且更易于浏览。

答案 4 :(得分:0)

Python 控制每个函数和每个模块的变量范围。

global 指令将函数中使用的变量的范围更改为全局变量。例如,如果所有代码都在同一个文件中,我们可以使用 global 指令。

def func():
    global x
    x = 1

func()
print(x)

哪个会打印 1

使用不同的模块,变量名将以模块名作为前缀。例如,如果我们有包含以下内容的 function.py

def func():
    global x
    x = 1

main.py 需要使用模块名称访问 x。

import functions

functions.func()
print(functions.x)

最后,使用全局变量可能会导致维护问题。因此,如果可能的话,我会考虑避免使用全局变量。