+ 1
if (dict.get(char)!=None): can someone explain me this line of logic
3 Antworten
+ 4
Assuming <dict> was a dictionary ...
get() method of `dict` class accepts one mandatory parameter (key), and a second optional parameter (default return value). When the default return value argument is omitted, a `None` object is returned, by default.
The line in your question calls the get() method passing only the mandatory argument (<char>). A `None` object is supposedly returned, when there's no item having such key existed in the dictionary <dict>. That is the default behaviour when nothing was passed as the optional second parameter (default return value) of the get() method.
The return value from get() method is then evaluated to see whether it was a `None` object by `if` conditional.
Note:
* Python doesn't require parentheses around conditional expression to be evaluated.
* Avoid using class name e.g. 'dict' for variable name.
+ 3
char looks like a variable that is storing a key that potentially belongs to a dictionary.
the dict.get() method returns the value paired with the key. in this scenario it is looking for the value paired with char, which is a variable storing the actual key. if the key does not exist, it returns None.
the != is not equal.
the if statement is read as:
if the return value of dict.get is not equal to None, then it is True.
i hope that makes sense. if not let me know where and i will try to do my beat to clear it up.
+ 3
Chinmay Shete Using pre-defined function names as variables mask them. You won't be able to access the 'dict()' function if you have another variable named 'dict'. If you really want to use it, then make another variable to hold the reference of the 'dict' function like this:
to_dict = dict
dict = 56
# Hope this helps