How can I pass global variables into a function?

How can Python behave differently when I pass two seemingly identical variables to a function?

  • What I'm doing is this: #Just two directions: s = [0,1,0] t = [0,0,1] #Two ways of getting the center c1 = [0,1,0] c2 = test.point_to_space(point) p = draw.squarepoints(c1, s, t, 1) plane = mlab.mesh(*p) This works perfectly, but fails horribly if I instead pass c2 to squarepoints. c1==c2 returns True, and they are the same type. The error I'm getting is: TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' What can cause this? The squarepoints function looks like this (real ugly; I was just slamming something together quickly): def squarepoints(center, vec1, vec2, width): assert len(center) == len(vec1) == len(vec2) == 3, 'Dimension mismatch!' result = [] for i in xrange(len(center)): #Scale the edge vectors a = 0.5*width*vec1[i] b = 0.5*width*vec2[i] c = center[i] result.append([[c-a-b,c-a+b],[c+a-b, c+a+b]]) return result The other possible sinner is the point_to_space function. This isn't defined when the object is created, but only when I call a method inside it. It looks like this: def embed(self, space): self.space = space #maps coordinates to a point in embedded space self.point_to_space = lambda p: [i.subs(zip(self.crds, p)) for i in self.space] 'space' is just xyz-coordinates as functions of spherical ones, and 'crds' are the symbols (phi, theta) used by sympy to handle the spherical coordinates.

  • Answer:

    In general, c1 and c2 are different objects, with possibly different superclasses. Just because c1 == c2, does not mean the objects are strictly the same. Things to check for in object equivalency:     assert c1.__class__ == c2.__class__     assert c1 == c2 and c2 == c1 With container objects, this can be tricky, as `==` usually only recursively performs `==`, so containers of similar objects have to be manually tested. *if I had just one guess, c1 contains floating point numbers (python default) and c2 contains some other number type, say `int`*

Allen Polak at Quora Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.