![]() There are no questions about if this piece of code is correct. This makes the code easy to change and adapt if you have a new CSV file that represents None with another string than "" if csvfield = CSV_NONE: (Since CSV is entirely text based, it cannot represent None without using predefined keywords) You are parsing CSV files and want the empty string to be parsed as None This makes the code very readable if formfields.name = not_filled_in: You want to check if the user wrote something or not.Ī good variable name may be not_filled_in You are building a form where a user can enter values. You have to think about what the empty string is. This is not what "descriptive variable name" means.Ī good descriptive variable name is based on its context. It isn't.īefore you go and do empty_string = "" and think you have a great variable name to compare to. One may think that "empty_string" is a descriptive variable name. What you should do is compare to a descriptive variable name. You should never check against magical values (more commonly known as magical numbers) Clean code approachĭoing this: foo = "" is very bad practice. I find hardcoding(sic) "" every time for checking an empty string not as good. That said, in most cases all of the mentioned solutions will work correctly. This is not only theoretical work but might actually be relevant in real usage since I have seen frameworks and libraries subclassing str before and using myString is "" might return a wrong output there. Since this explicitly calls the _eq_() method of the empty string it cannot be fooled by overriding any methods in myString and solidly works with subclasses of str. ![]() The only way of doing this that really cannot be fooled is the one mentioned in the beginning: ""._eq_(myString). Relying on the falsyness of a string may not work if the object overrides _bool_(). That means, theoretically "" is not necessary interned and that is implementation dependant.Īlso, comparing strings using is in general is a pretty evil trap since it will work correctly sometimes, but not at other times, since string pooling follows pretty strange rules. The big problem with the identity check is that String Internment is (as far as I could find) that it is not standardised which strings are interned. And "" is interned from the start in CPython Also, since they are identity checks, the only reason why they work is because Python uses String Pooling (also called String Internment) which uses the same instance of a string if it is interned (see here: Why does comparing strings using either '=' or 'is' sometimes produce a different result?). They will both fail if myString is not actually a string but a subclass of string (both will return False). MyString is "" and "" is myString are equivalent. ![]() "" = myString also gets fooled if myString overrides _eq_(). MyString = "" and myString._eq_("") can fail if myString overrides _eq_() and _ne_(). Len(myString) = 0 can fail if myString is an object of a class that inherits from str and overrides the _len_() method. ![]() The only really solid way of doing this is the following: if ""._eq_(myString):Īll other solutions have possible problems and edge cases where the check can fail. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |