Python| How can I make this variable global without initializing it as 'global' -
I have this code here. After the 'I = 1' line, the code can be added to main_____ only one part. This script will execute several times and there may be some variables in it (maybe 'i', 'xy', 'var', anything can happen), every time we need to increase from 1. I have found it to work by declaring 'I' worldwide, but unfortunately, I can not keep it like this.
Is there a way in which I can act as 'I' within a above mentioned parameters a global variable?
DIF main ______ (): Try: Excluding I + = 1, NameError: i = 1 main ______ ()
< Div class = "post-text" itemprop = "text">
If you want to use global variables, you have to declare it in the global form. What's wrong with that?
If you need to store state between calls, then you should use a class
& gt; & Gt; & Gt; Square F (): ... def __init __ (self): ... self.i = 0 ... def __call __ (self): ... print self.i ... self.i + = 1. .. & gt; & Gt; & Gt; F = F ()> gt; & Gt; & Gt; F () 0 >> gt; & Gt; F () 1> gt; & Gt; & Gt; F () 2
Comments
Post a Comment