Home > Software engineering >  AttributeError: 'numpy.int64' object has no attribute '__dict__' but float64 wor
AttributeError: 'numpy.int64' object has no attribute '__dict__' but float64 wor

Time:08-05

I am facing a very strange issue : json.dumps(np.int64(1), default=lambda o: o.__dict__) returns : AttributeError: 'numpy.int64' object has no attribute '__dict__'

While json.dumps(np.float64(1), default=lambda o: o.__dict__) returns correctly : '1.0'

Only difference is going from int64 to float64... Any suggestion ?

CodePudding user response:

numpy.float64 inherits from float, so json.dumps handles instances like a regular float, and doesn't use your default callback.

numpy.int64 doesn't inherit from int (and cannot reasonably do so due to conflicting semantics and memory layout), so json.dumps tries to use your default callback, which fails because numpy.int64 instances don't have a __dict__.

  • Related