全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据分析与数据挖掘
331 0
2022-11-24

在函数内部更改或创建的任何变量都是本地的,如果它尚未声明为全局变量。要告诉Python,我们要使用全局变量,我们必须使用关键字“global”,如以下示例所示:

# This function modifies global variable 's'

def f():

global s

print s

s = "Look for Geeksforgeeks Python Section"

print s


# Global Scope

s = "Python is great!"

f()

print s

现在没有歧义。


输出:

Python is great!
Look for Geeksforgeeks Python Section.
Look for Geeksforgeeks Python Section.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群