I think you want this (iterate the key/value pair as n, o
using iteritems()
):
class SomeClass:
class_dict = {0:0,1:1,2:2,3:3}
newClass = SomeClass()
for n, o in newClass.class_dict.iteritems():
print(o)
Question
Simple question: is it possible to assign shorthand references when iterating in Python, like this (which doesn't work):
class SomeClass:
class_dict = {0:0,1:1,2:2,3:3}
newClass = SomeClass()
for n in newClass.class_dict as thedict: # assigning a shorthand reference "thedict" to newClass.class_dict
print(thedict[n])
La solution
I think you want this (iterate the key/value pair as n, o
using iteritems()
):
class SomeClass:
class_dict = {0:0,1:1,2:2,3:3}
newClass = SomeClass()
for n, o in newClass.class_dict.iteritems():
print(o)
Autres conseils
Also, if you wanted to pursue the original idea, you can simply assign the class_dict to a new name. Any changes made to either dictionary affect both as they are really the same dictionary in memory because of aliasing.
Eg.
class SomeClass:
class_dict = {0:0,1:1,2:2,3:3}
newClass = SomeClass()
thedict = newClass.class_dict
for n in thedict:
thedict[n] = thedict[n] + 1
print(thedict[n])
print(newClass.class_dict)
will output
1
2
3
4
{0: 1, 1: 2, 2: 3, 3: 4}