tags:

views:

495

answers:

7

Suppose I have a class with a string instance attribute. Should I initialize this attribute with "" value, or None is OK either?

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

or

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

Thanks

Edit: What I thought is that if I use "" as an initial value, I "declare" a variable to be of string type. And then I won't be able to assign any other type to it later. Am I right?

Edit: I think it's important to note here, that my suggestion was WRONG. And there is no problem to assign another type to a variable. I liked a comment of S.Lott: "Since nothing in Python is "declared", you're not thinking about this the right way."

+1  A: 

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'>
Well, if he checks for truth, then he'll miss that `""` might be a valid value. Better that he checks for `None`ness instead.
Paul Biggar
Since we don't know what the OP is checking for, neither you nor me can judge what is better for him. BTW, did you consider my clearly marked edit, that answers the OPs edit, when you downvoted my answer?
+1 for the link to Truth Value Testing.
Ölbaum
+4  A: 

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

Ikke
+10  A: 

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'>
wierob
+1: What is the *meaning* of None or `""`? The design choice depends entirely on which value is more meaningful.
S.Lott
+2  A: 

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.

Vinay Sajip
+1  A: 

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

Paul Biggar
Good comments, but you should avoid using `str` as a variable name, since it will shadow the built-in `str()` function.
Daniel Roseman
@Daniel Roseman: Good point, thanks.
Paul Biggar
+1  A: 

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, ...
Chris
Why? That' the job of the default parameter, so why do it twice? And why not be explicit about which value is the default? And, *additionally*, "" declares quite obviously that the function expects a string, which `None` doesn't tell you.
kaizer.se
+1  A: 

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.

Paul