Initialize a string variable in Python: “” or None?

前端 未结 11 457
鱼传尺愫
鱼传尺愫 2021-01-30 12:44

Suppose I have a class with a string instance attribute. Should I initialize this attribute with \"\" value or None? Is either

相关标签:
11条回答
  • 2021-01-30 12:47

    None is used to indicate "not set", whereas any other value is used to indicate a "default" value.

    Hence, if your class copes with empty strings and you like it as a default value, use "". If your class needs to check if the variable was set at all, use None.

    Notice that it doesn't matter if your variable is a string initially. You can change it to any other type/value at any other moment.

    0 讨论(0)
  • 2021-01-30 12:47

    Either might be fine, but I don't think there is a definite answer.

    • If you want to indicate that the value has not been set, comparing with None is better than comparing with "", since "" might be a valid value,
    • If you just want a default value, "" is probably better, because its actually a string, and you can call string methods on it. If you went with None, these would lead to exceptions.
    • If you wish to indicate to future maintainers that a string is required here, "" can help with that.

    Complete side note:

    If you have a loop, say:

    def myfunc (self, mystr = ""):
        for other in self.strs:
            mystr = self.otherfunc (mystr, other)
    

    then a potential future optimizer would know that str is always a string. If you used None, then it might not be a string until the first iteration, which would require loop unrolling to get the same effects. While this isn't a hypothetical (it comes up a lot in my PHP compiler) you should certainly never write your code to take this into account. I just thought it might be interesting :)

    0 讨论(0)
  • 2021-01-30 12:52
    empty_string = ""
    
    if not empty_string:
    
        print "Empty string is not set"
    

    =>Empty string is not set

    if empty_string is not None:
        print "Empty string is not None"
    

    =>Empty string is not None

    0 讨论(0)
  • 2021-01-30 12:54

    Either is fine, though None is more common as a convention - None indicates that no value was passed for the optional parameter.

    There will be times when "" is the correct default value to use - in my experience, those times occur less often.

    0 讨论(0)
  • 2021-01-30 12:57

    It depends. If you want to distinguish between no parameter passed in at all, and an empty string passed in, you could use None.

    0 讨论(0)
  • 2021-01-30 12:59

    Another way to initialize an empty string is by using the built-in str() function with no arguments.

    str(object='')

    Return a string containing a nicely printable representation of an object.

    ...

    If no argument is given, returns the empty string, ''.

    In the original example, that would look like this:

    def __init__(self, mystr=str())
       self.mystr = mystr
    

    Personally, I believe that this better conveys your intentions.

    Notice by the way that str() itself sets a default parameter value of ''.

    0 讨论(0)
提交回复
热议问题