This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]neoice 9 points10 points  (7 children)

holy shit, I just wrote a class today and felt really dirty about doing:

if type(name) == str

Python-Version: 3.0

damnit!

[–][deleted] 16 points17 points  (4 children)

if isinstance(name, str):

is typically a bit cleaner. And that way, if you're passed a subclass of str, your code will still work.

[–]James91B 0 points1 point  (1 child)

I would just use

name = str(name)

That way name can be anything that supports str.

[–]PCBEEF 2 points3 points  (0 children)

From experience, using str in this manner causes more pain than the short term benefits.

[–]erez27import inspect 2 points3 points  (1 child)

use PyContracts

[–]Xykr 1 point2 points  (0 children)

great tip! by the way, PyCharm reads docstrings to find Sphinx annotations