Just some more things that I thought of. Since my last post received some attention, I thought I write a little more about things that I came accro...
For further actions, you may consider blocking this person and/or reporting abuse
Hi there,
good post, I remember when I crossed over from Java to Python and it took some time to adjust.
If we're talking about pythonic code, an interesting tidbit I once ran across is joining two dicts in a one liner.
I appreciate this article. I got me thinking about some of the ways I use python and made me want to make some improvements based on what you have here.
I was looking at your [0] and 0 examples and had a question/comment.
I was tinkering with your
foo
function and noticed that I could still pass a string in and it would be seen as valid. A string in Python has a length. If I called the function with a string,foo("hello")
for example, I would still get the output of "This list contains some items".I think it may be better to evaluate whether
data
is truly a list and not empty by doing something like this.What do you think? Am I missing something?
Hi,
Thanks for your comment, I think you are right. If you pass a string to
foo()
you also get some unexpected behavior. You already posted a remedy to the situation: checking the type of thedata
argument. That made me think, if there are other ways, that do not rely on type checking. (I know, type checking is not an expensive operation, but still...)Two more ways, you could alleviate the situation: Working with conventions, if the caller does not respect the contract, they get unexpected behavior:
Using type hinting: Since Python 3.5 you can specify the type of arguments, and static type checkers can use this information, to determine if you pass the correct types.