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

前端 未结 11 459
鱼传尺愫
鱼传尺愫 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:07

    Since both None and "" are false, you can do both. See 6.1. Truth Value Testing.

    Edit

    To answer the question in your edit: No, you can assign a different type.

    >>> a = ""
    >>> type(a)
    
    >>> a = 1
    >>> type(a)
    
    

提交回复
热议问题