calling class/static method from class variable in python

后端 未结 4 2013
遥遥无期
遥遥无期 2020-12-19 03:38

I\'m trying to make a ImageLoader class handle the loading and processing of image resources like this:

class ImageLoader:
    TileTable = __loadTileTable(\'         


        
4条回答
  •  醉梦人生
    2020-12-19 04:08

    Answering just the updated question, what you would do in Python is make TileTable a variable called tile_table in a module called imageloader. There is no reason at all to put any of this inside a class.

    So then you get:

    module1.py

    import imageloader
    aTile = imageloader.tile_table[1]
    

    module2.py

    import imageloader
    anotherTile = imageloader.tile_table[2]
    

    and imageload.py looks something like:

    def _loadTileTable(arg1, arg2):
        pass # blah blah
    tile_table = _loadTileTable('image path', other_var)
    

    Think of a Python module as a singleton instance in other languages (which in fact it is) and you'll be able to reconcile this with any OO preconceptions you inherited from other languages.

提交回复
热议问题