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'>
Answer from wierob on Stack OverflowIf 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'>
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 start with an empty string and concatenate, or start with an empty list and join.
barfoo = ''
barfoo += 'h'
barfoo += 'i'
print(barfoo)
...
barfoo = []
barfoo.append('h')
barfoo.append('i')
print(''.join(barfoo))
Here is an example of what you are trying to achieve:
barfoo = ""
barfoo = barfoo + 'H'
barfoo = barfoo + 'I'
Total curiosity, but is there a way I can do this better:
player_inputs = ["", ""]
I want to have a list of 2 strings, but the strings will be inputed later. I thought I could do this:
player_inputs = [str, str]
but that doesn't work, not sure why.
It has exactly the same effect, and '' (or "") should be used (similar [] for list, and () for tuple, {} for dict).
And Python will indeed have an internal global cache for small constants (short strings, including the empty string, and small numbers). Check that by id('') or id(0), it will very likely always return the same. (This is implementation specific, but CPython will behave this way.)
Use s = ''.
First, Python itself is not the fastest language in universe, and Python developers are not supposed to make such kind of optimization, like creating global variables for empty constants or optimizing variable initialization with constants. Your code will be considered as not maintainable, because others would not understand your purpose.
Second, any static code analyzer will always understand the type of your variable from a constant. But when function is called, analyzer has to find the declaration and get the return value type. I dont speak about str() - as a builtin, it's return type is well known to analyzers. But in general approach is not good, because in Python programmers not always explicitly define the return value type of functions.
# HINT: modify the headlines list to verify your loop works with different inputs
headlines = ["Local Bear Eaten by Man", "Legislature Announces New Laws", "Peasant Discovers Violence Inherent in System", "Cat Rescues Fireman Stuck in Tree", "Brave Knight Runs Away", "Papperbok Review: Totally Triffic"]
news_ticker = ""
write your loop here
n = 0 lenoftotal = 0 counter = 0
for n in headlines: counter = counter + 1 lenoftotal = lenoftotal + len(n) storedstring = storedstring + " " + headlines[n] if len(storedstring) == 140: break print(f"{storedstring}")
Output:
sentimental/ $ python mario.py
Traceback (most recent call last): File "/workspaces/23315992/sentimental/mario.py", line 18, in <module> storedstring = storedstring + " " + headlines[n] NameError: name 'storedstring' is not defined sentimental/ $
While initializing integer to 0 I can follow, not sure how to initialize a string type data and particularly storedstring in the above code.