Issue
This seems to work fine -
%time a = "abc"
print(a)
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 19.1 µs
abc
This doesn't -
def func():
%time b = "abc"
print(b)
func()
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 31 µs
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-57f7d48952b8> in <module>()
3 print(b)
4
----> 5 func()
<ipython-input-8-57f7d48952b8> in func()
1 def func():
2 get_ipython().magic(u'time b = "abc"')
----> 3 print(b)
4
5 func()
NameError: global name 'b' is not defined
Here's a link to a notebook
I'm using python 2.7, haven't tried it with python3 yet.
Is this expected behaviour?
Solution
I am almost certain this is an IPython bug. Reported here.
In [31]: def func():
...: a = 2
...: %time b = 1
...: print(locals())
...: print a
...: print b
In [32]: func()
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 6.2 µs
{'a': 2, 'b': 1}
2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-32-08a2da4138f6> in <module>()
----> 1 func()
<ipython-input-31-13da62c18a7e> in func()
4 print(locals())
5 print a
----> 6 print b
7
8
NameError: global name 'b' is not defined
Answered By - Christian Ternus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.