Thanks for this thing.
http://www.python.org/doc/current/lib/module-dis.html made it more
clear.
Fredrik Lundh wrote:
> David Harvey wrote:
>
> > Suppose I write
> >
> > if x in ("abc", "def", "xyz"):
> > doStuff()
> >
> > elif x in ("pqr", "tuv", "123"):
> > doOtherStuff()
> >
> > elif ...
>
Fredrik Lundh wrote:
>
> when in doubt, ask the compiler:
>
> def code(x):
> if x in ("abc", "def", "xyz"):
> doStuff()
> elif x in ("pqr", "tuv", "123"):
> doOtherStuff()
>
> import dis
> dis.dis(code)
>
> prints:
>
> 2 0 LOAD_FAST0 (x)
>
David Harvey wrote:
> if x in ("abc", "def", "xyz"):
> doStuff()
> elif x in ("pqr", "tuv", "123"):
> doOtherStuff()
> elif ...
If the code really looks like this:
# one-time-only code
big_dispatch_table = {}
for function, keys in [
(doStuff,
can you please explain how to read these output...I mean how to
understand them.
A quick glance tells you that the latter approach has less number of
instructions and thats why its better. Any more insight would help a
lot.
MTD wrote:
> For the sake of comparison:
>
> >>> def cod(x):
> ... tuppl
So nice to know this that you can compare your own code using this
methodology. I was completely unaware of this. Thank you so much.
Fredrik Lundh wrote:
> David Harvey wrote:
>
> > Suppose I write
> >
> > if x in ("abc", "def", "xyz"):
> > doStuff()
> >
> > elif x in ("pqr", "tuv", "123"):
> >
Fredrik Lundh wrote:
> when in doubt, ask the compiler:
MTD wrote:
> >>> dis.dis(cod)
Thanks so much guys! Python just gets cooler every day!
David
--
http://mail.python.org/mailman/listinfo/python-list
For the sake of comparison:
>>> def cod(x):
... tupple1 = ("abc", "def", "xyz")
... tupple2 = ("pqr", "tuv", "123")
... if x in tupple1:
... doStuff()
... elif x in tupple2:
... doOtherStuff()
...
>>> dis.dis(cod)
2 0 LOAD_CONST 7 (
David Harvey wrote:
> Suppose I write
>
> if x in ("abc", "def", "xyz"):
> doStuff()
>
> elif x in ("pqr", "tuv", "123"):
> doOtherStuff()
>
> elif ...
> When is python building the tuples? Does it need to build the tuple
> every time it comes through this code? Or does it somehow recognise
> that
Hi,
Suppose I write
if x in ("abc", "def", "xyz"):
doStuff()
elif x in ("pqr", "tuv", "123"):
doOtherStuff()
elif ...
etc.
When is python building the tuples? Does it need to build the tuple
every time it comes through this code? Or do