On Aug 11, 2010, at 2:47 PM, Christian Muise wrote:

> Cool.  So the next step is to make this work:
> 
> x = Symbol('x')
> assert ask(x, Q.integer) is None
> local_context = AssumptionsContext()
> local_context.add(Assume(x, Q.integer))
> with local_context:
>     assert ask(x, Q.integer) is True
> 
>   It doesn't really need any setup / tear-down...you mean to switch what the 
> global_assumptions points to during the 'with' block?

I am having trouble understanding what you are saying here.  Whatever you mean, 
what I'm saying is like the idea suggested at the top of issue 1047.

> 
> (by the way, I just noticed typing that that you should be testing "is 
> None/True/False" instead of "== None/True/False".)
> 
>   Even for the testing? I don't think I have any "== None/True/False" in the 
> main code base (was just following suit for the testing).

Well, it isn't terribly important, but it's better to use 'is' when you can, 
because it's faster (you are only comparing memory address).  Certain objects 
in Python, such as None, True, False, and types (like <type 'int'>) are unique, 
so will always work with 'is' comparison.  The same goes for singleton objects 
in SymPy (S.One, S.Zero, etc.).  

But like I said, it isn't too terribly important.  PEP 8 recommends it.  
Actually, the speed improvement isn't as much as I expected:

In [6]: %timeit a is None
10000000 loops, best of 3: 92.2 ns per loop

In [7]: %timeit a == None
10000000 loops, best of 3: 143 ns per loop

A much worse blunder is using 'is' when '==' should be used.  For example, 
CPython optimizes small integers, so this works:

a = 1
assert a is 1

but in other Python implementations, such as PyPy, it doesn't.  We actually 
have a this in a few places in the code, as someone was telling me on IRC a 
while back that he couldn't get it to work in PyPy because of it (I can't 
remember if I ever opened an issue for this).  

Aaron Meurer

> 
>   Cheers



-- 
You received this message because you are subscribed to the Google Groups 
"sympy-patches" group.
To post to this group, send email to sympy-patc...@googlegroups.com.
To unsubscribe from this group, send email to 
sympy-patches+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy-patches?hl=en.

Reply via email to