来自另一个类的python访问数组

时间:2014-09-16 20:21:35

标签: python-2.7

好日子的家伙们。我在从VideoForm类调用 filespath 数组时遇到问题。意见和建议将受到高度赞赏。感谢。

示例代码:

import os
from ffvideo import VideoStream
filespath = []
vidDuration = []
vidCode = [] 
class getVidInfo(QtCore.QThread):
      def __init__(self):
          AtCore.QThread.__init__(self)
      def run(self):
          for x in xrange(14)
             filename = filespath[x]
             filename = os.path.basename(filename)
             vs = VideoStream(filename)
             vidDuration.append(vs.duration)
             vidCodec.append(vs.codec_name)
class VideoForm(AtGui.MainWindow)
      def getVideoFile(self):
          for dirName, subDirList, fileList in os.walk(rootDir)
              for fname in fileList:
                  ext = fname[-3:]
                  if ext in ['flv','mp4','avi','mkv']
                     filespath.append(os.path.join(dirName, fname))

1 个答案:

答案 0 :(得分:0)

filespath是一个"全球"变量,在这种情况下类方法范围之外。如果要在类方法中修改它,则需要在方法中明确声明变量为全局:

class VideoForm(AtGui.MainWindow)
    def getVideoFile(self):
        global filespath # declare explicitly the intention of using the global one.
        for dirName, subDirList, fileList in os.walk(rootDir)
            for fname in fileList:
            ext = fname[-3:]
            if ext in ['flv','mp4','avi','mkv']
                filespath.append(os.path.join(dirName, fname))

虽然不建议这样做,除非你知道自己在做什么。例如,最好将列表作为参数传递给类构造函数。