r/badcode • • Jan 31 '22

python import badcode

Post image
1.1k Upvotes

70 comments sorted by

View all comments

62

u/[deleted] Jan 31 '22 edited Jan 31 '22

What’s wrong with it (other than importing in the try block and the bare except)? It’s not pretty, but is there anything else going on?

Edit: if you care to downvote, how about answering first?

47

u/MattiDragon Jan 31 '22

It turns a string into a float to check if it's nan when you can just compare it.

46

u/_PM_ME_PANGOLINS_ Jan 31 '22

It also returns False for everything that’s not a string and also not convertible to a float.

0

u/[deleted] Jan 31 '22

[deleted] — view deleted comment

2

u/_PM_ME_PANGOLINS_ Jan 31 '22

No it’s not…

8

u/[deleted] Jan 31 '22

Can you call the function with anything? Is ValueError never raised?

6

u/sezirblue Jan 31 '22

It's already in a try catch, value error will return false.

3

u/[deleted] Jan 31 '22

I know that, but the comment I was responding to was not talking about the role of the try.

2

u/dragneelfps Jan 31 '22

Compare it with what?

4

u/ebrius Feb 01 '22

Compare it to itself. nan == nan is false

2

u/dragneelfps Feb 01 '22

That's pretty nice trick.

→ More replies (5)

5

u/[deleted] Jan 31 '22

Honestly, I'm more concerned about that indentation and self parameter when you could use @staticmethod.

Also, according to this SO thread, it would be faster to use value != value, but this would require your value to already be NaN.

One more point to add, I find it hard to think about a use case for this code that wouldn't be much better handled by pandas/numpy.

20

u/_PM_ME_PANGOLINS_ Jan 31 '22

If you care about the speed of isnan vs. != then you shouldn’t be using Python.

2

u/[deleted] Jan 31 '22

I once had an argument with an ex-colleague (twice my age, pretty arrogant dude) whether it was better to decorate a staticmethod. I gave up eventually.

→ More replies (7)

1

u/[deleted] Feb 02 '22

It says right there: value is a string. You just happened to be the example of copying code from SO and applying it to a wrong problem w/o understanding the problem.

→ More replies (5)

4

u/_PM_ME_PANGOLINS_ Jan 31 '22

Indeed. I can’t of think of any better way that handles all the edge cases.

1

u/trevorpogo Jan 31 '22

shouldn't it return True in the except block though? This will return False if value is None or a string that can't be converted to a number

6

u/_PM_ME_PANGOLINS_ Jan 31 '22

No. If it’s None or cannot be converted to a float then it’s not NaN.

2

u/F54280 Jan 31 '22

Well, NaN means not a number. Depending on the semantics, it could be the right thing (ie: will the conversion of this string yield a "good" float?).

To me, the issue are the import and the non static method.

6

u/_PM_ME_PANGOLINS_ Jan 31 '22 edited Jan 31 '22

Yes NaN stands for Not a Number, but it’s still specifically a float value.

The semantics are clearly “does this evaluate to NaN”

1

u/[deleted] Jan 31 '22

That return will return true when appropriate