试图了解python中的变量范围

时间:2018-11-11 08:10:35

标签: python

我在Python中提供了以下程序只是为了了解变量的范围,但出现错误:

notificationInit(userID) {
    this.oneSignal.startInit(*****, *****);
    this.oneSignal.setSubscription(true);
    this.oneSignal.sendTag('userid', userID);
    this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.InAppAlert);
    this.oneSignal.handleNotificationReceived().subscribe(data => 
      this.onPushReceived(data.payload)
    );
    this.oneSignal.handleNotificationOpened().subscribe(() => {
      console.log('opened notification');
      // do something when a notification is opened
    });

    this.oneSignal.endInit();
  }

错误:

count = 0
def hello_func():
    if(count == 5):
        return 0
    print("Hello")
    count+=1
    hello_func()

hello_func()  

您能解释我在做什么吗?以及如何在不更改上述程序结构的情况下声明count具有全局变量?

1 个答案:

答案 0 :(得分:0)

在函数中使用全局变量时,需要明确声明:

<context:component-scan base-package="..package name..">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

第三行中的count = 0 def hello_func(): global count if(count == 5): return 0 print("Hello") count+=1 hello_func() hello_func() 声明我正在使用全局变量。