Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a string variable in Python: "" or None?

Tags:

python

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

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

or

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

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

like image 854
legesh Avatar asked Sep 09 '09 07:09

legesh


People also ask

Is it necessary to initialize string?

If you are talking about std::string , you don't need to "initialize" it because it is automatically initialized to the empty string in its constructor. If you mean const char * or char * , then yes, you should initialize them because by default they point to garbage. Then you may consider remove it.

Can string be initialized as null?

Initializing a string variable to null simply means that it does not point to any string object. String s=null; Whereas initializing a string variable to “” means that the variable points to an object who's value is “”.

How do you initialize a variable in Python to None?

The None keyword is used to define a null variable or an object. In Python, None keyword is an object, and it is a data type of the class NoneType . We can assign None to any variable, but you can not create other NoneType objects. Note: All variables that are assigned None point to the same object.

How do you initiate an empty string in Python?

If you want to initialize empty strings and like it as a default value, use the “”. If you want to check if the variable was set, use None.


2 Answers

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'> 
like image 200
wierob Avatar answered Sep 22 '22 17:09

wierob


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

like image 30
Steven Liekens Avatar answered Sep 22 '22 17:09

Steven Liekens