how can i get the value '111'
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
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.
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'
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']
精彩评论