类变量引用本身? [英] Class variable reference itself?
问题描述
我是编码阶段(3个星期)的新手,并且一直在构建和重建RPG,以帮助自己学习所学的原理。但是,我似乎只停留在Python类的一个特定方面。
I am new to coding period (3-ish weeks) and have been building and rebuilding a RPG to help teach myself the principles that I have been learning. However, I seem to be stuck on one specific aspect of Python classes.
我为 worldLocation $ c $创建的类变量c>类的读法如下,其中
NORTH
, EAST
, SOUTH
, WEST
所有参考字符串:
A class variable I have created for my worldLocation
class reads as such where the NORTH
, EAST
, SOUTH
, WEST
all reference strings:
WHITEROOM = worldLocation('A Plain White Room',
'...\n\nWhere are you?\n\n'
'All around you is white...\n\n'
'...only white...',
[ROCK.GROUNDDESC],
[RAT.NAME],
{NORTH: 'A Plain White Room',
SOUTH: 'A Plain White Room',
EAST: 'A Plain White Room',
WEST: 'A Plain White Room'})
但是我希望它改为这样,其中每个实例变量都引用了e类变量本身:
But I want it to instead read like this, where each of the instance variables references the class variable itself:
WHITEROOM = worldLocation('A Plain White Room',
'...\n\nWhere are you?\n\n'
'All around you is white...\n\n'
'...only white...',
[ROCK.GROUNDDESC],
[RAT.NAME],
{NORTH: WHITEROOM,
SOUTH: WHITEROOM,
EAST: WHITEROOM,
WEST: WHITEROOM})
但是,每次我尝试将其更改为后者时,都会收到未定义名称WHITEROOM
错误。我在做什么错还是想念?
However, every time I attempt to change it to the latter, I receive an undefined name WHITEROOM
error. What am I doing wrong or missing?
课程代码如下:
class worldLocation(object):
def __init__(self, NAME, DESC, GROUND, ENEMIES, DIRECTIONS):
self.NAME = NAME
self.DESC = DESC
self.GROUND = GROUND
self.ENEMIES = ENEMIES
self.DIRECTIONS = DIRECTIONS
推荐答案
您要使用的代码试图引用字典中的实例作为参数传递给 __ init __()
之前存在。那是行不通的(如果考虑的话,那可能真的很合理)。
The code you want to use is trying to reference the instance in a dictionary being passed as argument to __init__()
before it exists. That's not feasible (and arguably really reasonable if you think about it).
但是,您可以通过创建类初始化器<$ c $的特殊值来解决此问题。 c> __ init __()方法可以检查并修复传递的数据。
However, you can workaround the issue by creating a special value that the class initializer __init__()
method can check for and fix-up the data being passed.
下面的代码说明了我的意思。 注意我在开始时添加了一些脚手架,使其可以运行以用于测试。
Below is code illustrating what I mean. Note I've added some scaffolding at the beginning to make it runnable for testing purposes.
#### Some scaffolding for testing.
NORTH, SOUTH, EAST, WEST = 'NORTH SOUTH EAST WEST'.split()
class ROCK:
GROUNDDESC = 'ground desc'
class RAT:
NAME = 'Ricky'
####
class WorldLocation:
THIS = object() # A unique value.
def __init__(self, NAME, DESC, GROUND, ENEMIES, DIRECTIONS):
self.NAME = NAME
self.DESC = DESC
self.GROUND = GROUND
self.ENEMIES = ENEMIES
# Replace any values in DIRECTIONS dictionary argument that refer to the
# instance being created (as designated by them being the class
# attribute THIS).
self.DIRECTIONS = {direction: self if value is self.THIS else value
for direction, value in DIRECTIONS.items()}
WHITEROOM = WorldLocation('A Plain White Room',
'...\n\nWhere are you?\n\n'
'All around you is white...\n\n'
'...only white...',
[ROCK.GROUNDDESC],
[RAT.NAME],
{NORTH: WorldLocation.THIS,
SOUTH: WorldLocation.THIS,
EAST: WorldLocation.THIS,
WEST: WorldLocation.THIS})
# Show that it works.
from pprint import pprint
print(f'id(WHITEROOM): 0x{id(WHITEROOM):08x}\n')
pprint(vars(WHITEROOM))
示例输出:
id(WHITEROOM): 0x010d0390
{'DESC': '...\n'
'\n'
'Where are you?\n'
'\n'
'All around you is white...\n'
'\n'
'...only white...',
'DIRECTIONS': {'EAST': <__main__.WorldLocation object at 0x010D0390>,
'NORTH': <__main__.WorldLocation object at 0x010D0390>,
'SOUTH': <__main__.WorldLocation object at 0x010D0390>,
'WEST': <__main__.WorldLocation object at 0x010D0390>},
'ENEMIES': ['Ricky'],
'GROUND': ['ground desc'],
'NAME': 'A Plain White Room'}
这篇关于类变量引用本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!