using execfile() in class/instance methods

shindich at my-deja.com shindich at my-deja.com
Fri Dec 8 21:06:41 EST 2000


In article <slrn932qna.rtk.wcuddy at lserv.ja10629.home>,
  wcuddy at lserv.ja10629.home (Wayne Cuddy) wrote:
> I am attempting to use the execfile() function to "source" a file
that
> has valid python code to create a diction object:
>
> bn { 'key' : 'value1',
>         'key2' : 'value2' }
>
> I am issueing this command from a class constructor method and would
> like bn to be bound the class instance.  So in the future I can say:
>         print self.bn['key']
>
> Is this possible?  I assume I am screwing up the global() and locals
()
> arguments to the function which I can say that I don't fully
> understand.  Also is there another better/worse way to do this???just
> so I can see what else is out there..
>
> Thanks in advance,
>
> Wayne
>
>
Try the following:
>>> class Foo: pass

>>> f = Foo ()
>>> exec "f.bn = { 'key' : 'value1',  'key2' : 'value2' }"
>>> f.bn
{'key': 'value1', 'key2': 'value2'}

I hope this helps!

Regards,

Alex Shindich
visit http://www.shindich.com/


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list