Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么全局字典可以在类内访问,而全局整型变量则不能?_Python_Class_Oop - Fatal编程技术网

Python 为什么全局字典可以在类内访问,而全局整型变量则不能?

Python 为什么全局字典可以在类内访问,而全局整型变量则不能?,python,class,oop,Python,Class,Oop,我已经声明了一个全局字典和一个变量。现在,当在一个类中访问这两个变量时,我可以访问字典,但对于访问另一个变量,我会得到UnboundLocalError。为了解决这个问题,我使用了代码行:global curr\u length,然后访问它并运行它。但我想知道,为什么一个普通整数变量需要这一行额外的代码,而字典却不需要 代码是: memoized = {} curr_length = 0 curr_pal = "" class Solution: def check_palindro

我已经声明了一个全局字典和一个变量。现在,当在一个类中访问这两个变量时,我可以访问字典,但对于访问另一个变量,我会得到
UnboundLocalError
。为了解决这个问题,我使用了代码行:
global curr\u length
,然后访问它并运行它。但我想知道,为什么一个普通整数变量需要这一行额外的代码,而字典却不需要

代码是:

memoized = {}
curr_length = 0
curr_pal = ""


class Solution:
    def check_palindrome(self, str_):
        if len(str_) <= 1:
            return False
        global curr_length
        if len(str_) <= curr_length:
            return False
        if memoized.get(str_, None):
            return memoized[str_]
        if str_ == str_[::-1]:
            memoized[str_] = True
            curr_length = len(str_)
            return True
        memoized[str_] = False
        return False

    def longest_palindrome(self, str_, starting_index):
        if len(str_) <= 1:
            return None
        n = len(str_)
        if self.check_palindrome(str_[starting_index:n]):
            return str_
        n -= 1
        self.longest_palindrome(str_[starting_index:n], starting_index)

    def longestPalindrome(self, s: str) -> str:
        for starting_index in range(len(s)):
            pal = self.longest_palindrome(s, starting_index)
            if pal:
                return pal
        return ""


solution = Solution()
print(solution.longestPalindrome("babad"))
memonized={}
当前长度=0
curr_pal=“”
类解决方案:
def check_回文(self,str_):
如果len(str_u2;)短:
您正试图在一个函数中使用
curr\u length=len(str\u)
更改
curr\u length
的值,该函数查找本地
curr\u length
变量,但找不到它。它需要行
global curr\u length
才能知道它是一个
global
变量

至于你为什么想知道为什么一个
dict
对象不需要
global memorized
行,你应该阅读以下答案: 或

说明: 在Python中,在函数外部或全局范围内声明的变量称为全局变量。这意味着,可以在函数内部或外部访问全局变量

让我们看一个关于如何在Python中创建全局变量的示例

x = "global"

def foo():
    print("x inside :", x)

foo()
print("x outside:", x)
当我们运行代码时,输出将是:

x inside : global
x outside: global
在上面的代码中,我们创建了x作为全局变量,并定义了一个foo()来打印全局变量x。最后,我们调用foo(),它将打印x的值

如果要更改函数中x的值,该怎么办

def foo():
    x = x * 2
    print(x)
foo()
当我们运行代码时,输出将是:

x inside : global
x outside: global
UnboundLocalError:赋值前引用的局部变量“x”

输出显示错误,因为Python将x视为局部变量,并且x也没有在foo()中定义

为了实现这一点,我们使用关键字

Short: 您正试图在一个函数中使用
curr\u length=len(str\u)
更改
curr\u length
的值,该函数查找本地
curr\u length
变量,但找不到它。它需要行
global curr\u length
才能知道它是一个
global
变量

至于你为什么想知道为什么一个
dict
对象不需要
global memorized
行,你应该阅读以下答案: 或

说明: 在Python中,在函数外部或全局范围内声明的变量称为全局变量。这意味着,可以在函数内部或外部访问全局变量

让我们看一个关于如何在Python中创建全局变量的示例

x = "global"

def foo():
    print("x inside :", x)

foo()
print("x outside:", x)
当我们运行代码时,输出将是:

x inside : global
x outside: global
在上面的代码中,我们创建了x作为全局变量,并定义了一个foo()来打印全局变量x。最后,我们调用foo(),它将打印x的值

如果要更改函数中x的值,该怎么办

def foo():
    x = x * 2
    print(x)
foo()
当我们运行代码时,输出将是:

x inside : global
x outside: global
UnboundLocalError:赋值前引用的局部变量“x”

输出显示错误,因为Python将x视为局部变量,并且x也没有在foo()中定义


为了实现这一点,我们使用关键字

从方法中删除
全局curr\u length
时会发生什么?从方法中删除
全局curr\u length
时会发生什么?您需要添加更多关于如何使用
全局
的说明。一开始,OP正在按照你的链接建议使用
curr\u length
变量。答案是试图解释他试图用
curr\u length=len(str\u)
更改
curr\u length
的值,那么这与他们的字典有什么关系呢?OP使用了
global
关键字
curr\u length
,正如您的链接所建议的,您需要添加更多关于如何使用
global
的说明。一开始,OP正在按照你的链接建议使用
curr\u length
变量。答案是试图解释他试图用
curr\u length=len(str\u)
更改
curr\u length
的值,那么这与他们的字典有什么关系呢?OP使用了
global
关键字
curr\u length
,正如你的链接所暗示的那样