2

EDIT: Here is the actual code I tried that failed:

sounds.py

import audio
import time

localAudioPlayer = None

def Play(soundString, wait=True):
    if (localAudioPlayer != None): 
        localAudioPlayer.stop()
    localAudioPlayer = audio.stream("sound/%s.ogg" % soundString)
    localAudioPlayer.play()
    if (wait == True):
        while (localAudioPlayer.playing == True):
            time.sleep(0.1)
    return

"audio" is a complete library I wrote (in a folder with an init) that allows audio playback.

The idea here is that if Play() is called while a sound is already playing, that sound should be stopped.

I don't have my code setup in such a way that I can instantiate the audio.stream() object without having an actual file to play, so pre-initializing it isn't really a good idea.

I tried similar code with my original example (I set stuffLocalVar = None then tested it for None in the function) and it worked fine. So it is something specific to this particular code.

When I did "import sounds" at the Python console and tried to execute Play() directly, I got the same traceback.

Traceback:

>>> sounds2.Play("file.ogg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sounds2.py", line 7, in Play
    if (localAudioPlayer != None): 
UnboundLocalError: local variable 'localAudioPlayer' referenced before assignment

Original

I'm not sure of the proper terminology for this setup, so let me give you a short example:

mainApp.py:

import stuff
print stuff.do() # should print 16
stuff.stuffLocalVar = 8
print stuff.do() # should print 32

stuff.py

stuffLocalVar = 4
def do():
    return stuffLocalVar * 4

Is this possible to do? I want to do this because the stuff.py (this is hugely simplified just to emphasize the point of the question) contains code that I don't want the user to be able to instantiate multiple classes of. There needs to be only one "instance" of this code, app-wide. But the functions in stuff.py depend on data retained within that section of code.

4

2 回答 2

7

对的,这是可能的。你刚刚做到了。

--

重新更新:这是一个完全不同的问题,显示完全不同的代码。

您的代码的问题是您有两个不同的变量:

outervar = None # this is global

def foo():
    if(outervar): #this refers to the local variable
       outervar = "you fail it" # this creates a local variable

你想要的是:

outervar = None # this is global

def foo():
    global outervar # prevents creation of local variable called outervar
    if(outervar):
       outervar = "you win it" # this assigns to global variable
于 2013-05-16T01:14:07.780 回答
1

这就是我喜欢在没有类的 python 函数中模仿持久/静态变量的方式。这是一个简单的例子来演示。在这种情况下,“静态”变量“islicensed.value”用于确保我们只读取一次注册表,无论我们调用函数 islicensed() 多少次。我更喜欢避免使用全局变量,这似乎更 Pythonic。

def islicensed(): 

    try:  # trick to mimic a persistent/static variable so I don't have to read the registry but once
        return islicensed.value
    except AttributeError:
        # read registry for license values
        settings = QtCore.QSettings("company", "myapp")
        license = str(settings.value("license"))

        if license == "somevalue":
            islicensed.value = True
        else:
            islicensed.value = False

        return islicensed.value
于 2014-07-11T20:35:46.203 回答