在 Python 中初始化一个字符串变量: “”还是“无”?

假设我有一个具有 绳子实例属性的类。 我应该用 ""值还是 没有初始化这个属性?

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

或者

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

编辑 : 我的想法是,如果我使用 ""作为初始值,我将“ 声明”作为 绳子类型的变量。然后我就不能给它分配任何其他类型了。我说的对吗?

编辑 : 我认为在这里需要注意的是,我的建议是错误的。将另一种类型赋给变量也没有问题。我喜欢 S.Lott的一个评论: “ 由于 Python 中没有任何内容是“声明的”,因此您没有以正确的方式考虑这个问题。

384006 次浏览

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)
<type 'str'>
>>> a = 1
>>> type(a)
<type 'int'>

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

If not having a value has a meaning in your program (e.g. an optional value), you should use None. That's its purpose anyway.

If the value must be provided by the caller of __init__, I would recommend not to initialize it.

If "" makes sense as a default value, use it.

In Python the type is deduced from the usage. Hence, you can change the type by just assigning a value of another type.

>>> x = None
>>> print type(x)
<type 'NoneType'>
>>> x = "text"
>>> print type(x)
<type 'str'>
>>> x = 42
>>> print type(x)
<type 'int'>

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.

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 :)

For lists or dicts, the answer is more clear, according to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#default-parameter-values use None as default parameter.

But also for strings, a (empty) string object is instanciated at runtime for the keyword parameter.

The cleanest way is probably:

def myfunc(self, my_string=None):
self.my_string = my_string or "" # or a if-else-branch, ...

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.

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 ''.

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

>>>x = None
>>>print(x)
None
>>>type(x)
<class 'NoneType'>
>>>x = "hello there"
>>>print(x)
hello there
>>>type(x)
<class 'str'>
>>>
>>>x = ""
>>>print(x)


>>>type(x)
<class 'str'>
>>>x = "hello there"
>>>type(x)
<class 'str'>
>>>print(x)
hello there
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

Python philosophy is to be readable.
That's why it's good practice to define your attributes in __init__() even if it's optional.
In the same spirit, you have to ask yourself what clearer for anyone who reads your code. In fact the type itself give much information about future use of your variable. So:

kind = None

Is syntaxically correct, but reader does not know much. Is it a string, a code as integer, a list, etc. ?

kind_str = None
kind = ""

Both say a little more, the first has the type in its name and the second in its declaration. I would go for the second, neater.