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

前端 未结 11 585
鱼传尺愫
鱼传尺愫 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 13:08

    Either way is okay in python. I would personally prefer "". but again, either way is okay

    >>>x = None
    >>>print(x)
    None
    >>>type(x)
    
    >>>x = "hello there"
    >>>print(x)
    hello there
    >>>type(x)
    
    >>> 
    >>>x = ""
    >>>print(x)
    
    >>>type(x)
    
    >>>x = "hello there"
    >>>type(x)
    
    >>>print(x)
    hello there
    

提交回复
热议问题