从python中的类变量调用类/静态方法 [英] calling class/static method from class variable in python

查看:997
本文介绍了从python中的类变量调用类/静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让ImageLoader类处理图像资源的加载和处理,如下所示:

  class ImageLoader: 
TileTable = __loadTileTable('image path',一些其他变量)

@staticmethod
def _loadTileTable(arg1,arg2):
blah blah

但是,在编译时我得到: NameError:name'_loadTileTable' $ c>



如果我用 TileTable = ImageLoader .__ loadTileTable('image path',一些其他变量)替换第二行然后我得到 NameError:name'ImageLoader'未定义



到Python,静态类与静态方法是我会用来实现这一点。但是,我打开一下我一般在python(即,调用静态库函数,只通过其功能分组在一起)这样做。



UPDATE:
读完这两个答案后,我得到了一张图片,我想要做的可能是不对的。
我将如何实现ImageLoader,以便我可以这样做:



假设tile表返回一个数组



module1.py

  aTile = ImageLoader.TileTable [1] 

module2.py

  anotherTile = ImageLoader.TileTable [2] 



感谢所有的答案,我发现我的最后一个答案填充TileTable只有一次在python模块doco


模块可以包含可执行
语句以及函数
定义这些语句是
用于初始化模块
它们只在第一次执行
模块被导入到某处


对于静态类,我要放弃类,只是做一个模块级变量。

解决方案

回答刚才更新的问题,你会在Python中做什么是 TileTable 一个变量 tile_table imageloader 的模块中。



那么你得到:



module1.py

  import imageloader 
aTile = imageloader.tile_table [1]



module2.py

  import imageloader 
anotherTile = imageloader.tile_table [2]


$ b b

imageload.py 看起来像:

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

将Python模块视为其他语言的单例实例(实际上是这样),您可以将其与任何从其他语言继承的OO先验概念进行协调。


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

class ImageLoader:
    TileTable = __loadTileTable('image path', some other variable)

    @staticmethod
    def _loadTileTable(arg1, arg2):
        blah blah

however, on compile i get: NameError: name '_loadTileTable' is not defined

If i replace the second line with TileTable = ImageLoader.__loadTileTable('image path', some other variable) then i get NameError: name 'ImageLoader' is not defined

As i'm going from C# to Python, static classes with static methods is what i'd use to implement this. However, i'm open to how I'd do this in general in python (that is, call static library functions that are only grouped together by their functionality).

UPDATE: After reading both answers, I'm getting a picture that what i'm trying to do probably isn't right. How would I go about imlementing ImageLoader so that I can do this:

Assuming that tile table returned an array

module1.py

aTile = ImageLoader.TileTable[1]

module2.py

anotherTile = ImageLoader.TileTable[2]

ideally, i'd populate TileTable just once.

Update:

Thanks for all the answers, I found my last answer to populating TileTable just once in the python modules doco

"A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module is imported somewhere"

As for static class, i'm going to forgo classes and just make a module level variable.

解决方案

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.

这篇关于从python中的类变量调用类/静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆