Re: Changing functions in a running program?
Andreas Yankopolus <[email protected]> Mon, 14 Jan 2019 10:16:53 -0500
| Newsgroups | gmane.comp.python.ipython.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============1342023281085279464==
Content-Type: multipart/alternative;
boundary="Apple-Mail=_674DC4BF-0CBA-4B09-BFB3-451E81860677"
--Apple-Mail=_674DC4BF-0CBA-4B09-BFB3-451E81860677
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=utf-8
Thomas,
> If you define a function or variable at the breakpoint, it's probably =
making it a local variable inside main(), so it's not in scope for =
name().
>=20
> You may be able to get round this by defining the new function and =
then explicitly making it a global variable, something like this:
>=20
> globals()['print_name'] =3D print_name
>=20
> Not exactly elegant, but hopefully it works.
Yes=E2=80=94makes sense and does the trick! I=E2=80=99ll have to figure =
out an Emacs hook to automatically update globals in that manner. My =
output:
In main.
> /Users/ayank/Documents/programming/python/bar.py(13)main()
12 import ipdb; ipdb.set_trace()
---> 13 name()
14=20
ipdb> name()
Alice
ipdb> !def print_name(): print ("Bob")
ipdb> name()
Alice
ipdb> globals()['print_name'] =3D print_name
ipdb> name()
Bob
ipdb>=20
To follow up on Wes=E2=80=99s comment regarding TDD=E2=80=94I=E2=80=99m =
writing signal processing code and using Python + SciPy like Matlab. =
There are some calculations when the code starts that take a few =
minutes. I have a breakpoint there and am experimenting with the next =
steps of the processing chain.=
--Apple-Mail=_674DC4BF-0CBA-4B09-BFB3-451E81860677
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
charset=utf-8
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" =
class=3D"">Thomas,<br class=3D""><div><br =
class=3D""></div><div><blockquote type=3D"cite" class=3D""><div =
class=3D""><div class=3D"">If you define a function or variable at the =
breakpoint, it's probably making it a local variable inside main(), =
so it's not in scope for name().<br class=3D""><br class=3D"">You may be =
able to get round this by defining the new function and =
then explicitly making it a global variable, something like =
this:<br class=3D""><br class=3D"">globals()['print_name'] =3D =
print_name<br class=3D""><br class=3D"">Not exactly elegant, but =
hopefully it works.<br class=3D""></div></div></blockquote><div><br =
class=3D""></div><div>Yes=E2=80=94makes sense and does the trick! I=E2=80=99=
ll have to figure out an Emacs hook to automatically update globals in =
that manner. My output:</div><div><br class=3D""></div><div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D"">In main.</div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo; color: rgb(47, 180, 29);" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #000000" class=3D"">> </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">/Users/ayank/Documents/programming/python/bar.py</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #000000" =
class=3D"">(13)</span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2eaebb" class=3D"">main</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #2fb41d" class=3D""> 12 </span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #b42419" =
class=3D""> </span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2fb41d" class=3D"">import</span><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D""> =
ipdb</span><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #400bd9" class=3D"">;</span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""> ipdb</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">.</span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">set_trace</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(47, 180, 29);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">---> 13 </span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #b42419" =
class=3D""> </span><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #000000" class=3D"">name</span><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #400bd9" =
class=3D"">()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo; color: =
rgb(47, 180, 29);" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""> =
14 </span></div><div style=3D"margin: 0px; font-stretch: normal; =
font-size: 11px; line-height: normal; font-family: Menlo; min-height: =
13px;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D""></span><br class=3D""></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #2d961e" =
class=3D"">ipdb> </span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">name()</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">Alice</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2d961e" class=3D"">ipdb> </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">!def =
print_name(): print ("Bob")</span></div><div style=3D"margin: 0px; =
font-stretch: normal; font-size: 11px; line-height: normal; font-family: =
Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures; color: #2d961e" class=3D"">ipdb> </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">name()</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">Alice</span></div><div style=3D"margin: 0px; font-stretch: =
normal; font-size: 11px; line-height: normal; font-family: Menlo;" =
class=3D""><span style=3D"font-variant-ligatures: no-common-ligatures; =
color: #2d961e" class=3D"">ipdb> </span><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">globals()['print_name'] =3D print_name</span></div><div =
style=3D"margin: 0px; font-stretch: normal; font-size: 11px; =
line-height: normal; font-family: Menlo;" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures; color: #2d961e" =
class=3D"">ipdb> </span><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">name()</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo;" class=3D""><span style=3D"font-variant-ligatures: =
no-common-ligatures" class=3D"">Bob</span></div><div style=3D"margin: =
0px; font-stretch: normal; font-size: 11px; line-height: normal; =
font-family: Menlo; color: rgb(45, 150, 30);" class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" =
class=3D"">ipdb> </span></div><div class=3D""><span =
style=3D"font-variant-ligatures: no-common-ligatures" class=3D""><br =
class=3D""></span></div><div class=3D"">To follow up on Wes=E2=80=99s =
comment regarding TDD=E2=80=94I=E2=80=99m writing signal processing code =
and using Python + SciPy like Matlab. There are some calculations when =
the code starts that take a few minutes. I have a breakpoint there and =
am experimenting with the next steps of the processing =
chain.</div></div></div></body></html>=
--Apple-Mail=_674DC4BF-0CBA-4B09-BFB3-451E81860677--
--===============1342023281085279464==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
IPython-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/ipython-dev
--===============1342023281085279464==--