【问题标题】:Access variable from a different function from another file从另一个文件的不同函数访问变量
【发布时间】:2021-11-13 00:07:57
【问题描述】:

我在 mypythonlib 目录中有一个文件 myfunctions.py

from requests_html import HTMLSession
import requests

def champs_info(champname:str, tier_str:int):  
    url = f"https://auntm.ai/champions/{champname}/tier/{tier_str}"
    session = HTMLSession()
    r = session.get(url)
    r.html.render(sleep=1, keep_page=True, scrolldown=1)

    information = r.html.find("div.sc-hiSbYr.XqbgT")
    sig = r.html.find('div.sc-fbNXWD.iFMyOV')
    tier_access = information[0]
    tier = tier_access.text

我想通过另一个文件-test_myfunctions.py访问变量tier 但问题是我还必须为函数champs_info 提供参数,以便它可以相应地访问url。

from mypythonlib import myfunctions
def test_champs_info():
    return myfunctions.champs_info("abomination",6).tier

但是在运行这段代码时,我得到了错误-

./tests/test_myfunctions.py::test_champs_info Failed: [undefined]AttributeError: 'NoneType' object has no attribute 'tier'
def test_champs_info():
>       return myfunctions.champs_info("abomination",6).tier
E       AttributeError: 'NoneType' object has no attribute 'tier'

tests/test_myfunctions.py:3: AttributeError

对此有任何解决方案,为什么此代码无法访问该变量? 我写了myfunctions.champs_info("abomination",6).tier,希望它能从champs_info函数中获取tier变量,同时从myfunctions文件中提供所有所需的参数:(

【问题讨论】:

    标签: python function nested-function


    【解决方案1】:

    您可以通过以下方式访问函数中变量的值:1) 返回函数中的值,2) 在模块中使用全局变量,或 3) 定义类。

    如果只想访问函数本地的单个变量,则函数应返回该值。类定义的一个好处是您可以定义任意数量的变量来访问。

    1.返回值

    def champs_info(champname:str, tier_str:int):  
        ...
        tier = tier_access.text
        return tier
    

    2。全球

    tier = None
    def champs_info(champname:str, tier_str:int):
        global tier
        ...
        tier = tier_access.text
    

    访问全局层级变量。

    from mypythonlib import myfunctions
    
    def test_champs_info():
        myfunctions.champs_info("abomination", 6)
        return myfunctions.tier
    
    print(test_champs_info())
    

    3.类定义:

    class Champ:
        def __init__(self):
            self.tier = None
        def champs_info(self, champname:str, tier_str:int):  
            ...
            self.tier = tier_access.text
    

    test_functions.py 可以通过这种方式调用 champs_info()。

    from mypythonlib import myfunctions
    
    def test_champs_info():
        info = myfunctions.Champ()
        info.champs_info("abomination", 6)
        return info.tier
        
    print(test_champs_info())
    

    【讨论】:

    • 使用类方法,我可以使用在champs_info内部创建嵌套函数并在文件外部访问它的方式吗?
    • @Dheeraj-Tech - 更新了在另一个脚本/类中调用 champs_info() 和访问层变量的方式。
    • 酷!这确实有效!非常感谢你帮助我!
    【解决方案2】:

    在 myfunctions.champs_info() 中添加 return tier

    并在脚本 test_myfunctions.py 中删除 .tier

    【讨论】:

    • 但是如果我想从 champs_info() 访问多个变量怎么办?
    • 我试图在champs_info() 中创建一个内部函数,这样如果我想返回一个变量,我就可以调用任何函数,但我也无法访问它:(
    • 函数中定义的 Python 变量不能在函数外部访问,除非它们附加到函数外部可访问的对象。您将不得不更改变量这样 champs_info.tier = 'xyz' ,并在函数外以相同的方式访问它们。
    • 或者如上所述使用全局变量。但是请确保无论何时使用全局变量,数据都不会稍后在脚本中的某个地方混在一起!
    【解决方案3】:

    您只需要从 champs_info() 函数返回层

    就像这样:

    myfunctions.py

    from requests_html import HTMLSession
    import requests
    
    def champs_info(champname:str, tier_str:int):  
        url = f"https://auntm.ai/champions/{champname}/tier/{tier_str}"
        session = HTMLSession()
        r = session.get(url)
        r.html.render(sleep=1, keep_page=True, scrolldown=1)
    
        information = r.html.find("div.sc-hiSbYr.XqbgT")
        sig = r.html.find('div.sc-fbNXWD.iFMyOV')
        tier_access = information[0]
        tier = tier_access.text
        return tier               # <---- Focus Here
    

    test_myfunctions.py

    import myfunctions
    
    print(temp.champs_info("americachavez", 6))
    

    就是这样。你已经完成了。

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多