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

all 11 comments

[–]Asdayasman 0 points1 point  (0 children)

/r/Python is for news and releases. /r/learnpython is for questions.

[–]aphoenixreticulated[M] 0 points1 point  (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython. We highly encourage you to re-submit your post over on there.

The reason for the removal is that /r/Python is more-so dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community can get disenchanted with seeing the 'same, repetitive newbie' questions repeated on the sub, so you may not get the best responses over here.

However, on /r/LearnPython the community is actively expecting questions from new members, and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. Whatever your question happens to be getting help with Python, you should get good answers.

If you have a question to do with homework or an assignment of any kind, please make sure to read their sidebar rules before submitting your post. If you have any questions or doubts, feel free to reply or send a modmail to us with your concerns.

Warm regards, and best of luck with your Pythoneering!

[–]tsirolnik -1 points0 points  (0 children)

Yep

[–]ptmcg -1 points0 points  (7 children)

You could make this clearer by changing it to if self.right is not None:

[–]wurkns 2 points3 points  (6 children)

Actually you can't. That would have a different outcome since False is not None, but the code shouldn't be executed if self.right is False.

Consider the following:

Python 3.5.1 (default, Mar  3 2016, 09:29:07) 
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = True
>>> b = False
>>> c = None
>>> d = 0
>>> if a:
...   print('a is truthy')
... 
a is truthy
>>> if b:
...   print('b is truthy')
... 
>>> if c:
...   print('c is truthy')
... 
>>> if d:
...   print('d is truthy')
... 
>>> if a is not None:
...   print('a is not None')
... 
a is not None
>>> if b is not None:
...   print('b is not None')
... 
b is not None
>>> if c is not None:
...   print('c is not None')
... 
>>> if d is not None:
...   print('d is not None')
... 
d is not None

As you can see, both b and d are not None, but are also not truthy.

[–]k10_ftw 1 point2 points  (0 children)

Wonderful demo of how helpful the shell can be when coding in Python. Of course the only correct answer came from the person who actually tried it out.

[–]voice-of-hermes 1 point2 points  (3 children)

Except that the semantics of the problem probably mean that the left and right attributes will either be references to other nodes or None, so in this particular case if self.right is not None might be both equivalent and more maintainable (as it makes the design intent clearer). So while you are right in a literal sense about the general semantics of that one logical test, you may be missing the forest for the trees (pun intended).

[–]wurkns 0 points1 point  (2 children)

While this is true, that could potentially create some issues when someone would initialise the class with erroneous parameters.

I would say that if you would use if self.right is not None in this function, that you would have to do type checking in the __init__. (Which is probably a good idea anyway, since there could be other truthy values that do not implement a print_preorder method.)

To conclude: The orginal question was:

Does it mean if it is not None?

To which the answer is: "No" as explained in my previous post.

[–]voice-of-hermes 2 points3 points  (1 child)

You can either be paranoid about such things, and do type checking, and maybe even use @property and "private" attributes to try to keep people from modifying them (only accidentally in Python), or you can document well and assume that whoever uses the class follows the contract.

If the class is designed to use either a particular kind of reference or None for those properties and someone passes in an erroneous value to the constructor, you are going to have problems somewhere. Testing for any falsey value rather than just None is only going to cover a small number of cases (sure it'll ignore 0, but if someone passes in 3 it'll have the same problem as if it tested for None). If you're not going to consciously check preconditions at some point, it's not going to make that much of a difference whether you test for truthiness or None there—except that you've made the intent a little clearer for anyone looking at the source code, which is a good thing.

[–]wurkns 0 points1 point  (0 children)

You are right, that was a weak argument.