python类之间变量互相调用,从python中的类变量调用类/静态方法

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值