tags:

views:

149

answers:

3
def a():
    w='www'
a.a='aaa'
print a.__dict__
a.__dict__={'1':'111','2':'222'}
print a.1#error
print a['1']#error

how can i get the value '111' thanks

+1  A: 

You can access it thanks to the __dict__ member. See following code

def a():
    w='www'
a.a='aaa'
print a.__dict__
a.__dict__={'1':'111','2':'222'}
print a.__dict__['1']
luc
+8  A: 

You'll have to do

print a.__dict__['1']

or

print getattr(a, '1')

"1" is not a valid variable name in Python. If you did:

a.__dict__ = {'a1' : '111'}
print a.a1

it would work.

Sapph
I wouldn't recommend messing with `__dict__` though. :)
Sapph
+2  A: 

Since you say are just a beginner, perhaps you're just looking for the even easier:

 a = {'1':'111','2':'222'}

so a['1'] returns the desired '111'

Andrew Jaffe