Questions tagged [nonetype]

Nonetype refers to an object or type whose value is null (none).

1345 questions
994
votes
9 answers

Python `if x is not None` or `if not x is None`?

I've always thought of the if not x is None version to be more clear, but Google's style guide and PEP-8 both use if x is not None. Are there any minor performance differences (I'm assuming not), and is there any case where one really doesn't fit…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
907
votes
4 answers

not None test in Python

Out of these not None tests. if val != None: if not (val is None): if val is not None: Which one is preferable, and why?
prosseek
  • 182,215
  • 215
  • 566
  • 871
750
votes
15 answers

Return a default value if a dictionary key is not available

I need a way to get a dictionary value if its key exists, or simply return None, if it does not. However, Python raises a KeyError exception if you search for a key that does not exist. I know that I can check for the key, but I am looking for…
Spyros
  • 46,820
  • 25
  • 86
  • 129
661
votes
9 answers

How to "test" NoneType in python?

I have a method that sometimes returns a NoneType value. So how can I question a variable that is a NoneType? I need to use if method, for example if not new: new = '#' I know that is the wrong way and I hope you understand what I meant.
CrveniZg
  • 6,671
  • 2
  • 12
  • 8
449
votes
10 answers

Why do I get AttributeError: 'NoneType' object has no attribute 'something'?

I am getting an error message that says AttributeError: 'NoneType' object has no attribute 'something' How can I understand this message? What general scenarios might cause such an AttributeError, and how can I identify the problem? This is a…
Jacob Griffin
  • 4,807
  • 3
  • 16
  • 11
326
votes
11 answers

remove None value from a list without removing the 0 value

This was my source I started with. My List L = [0, 23, 234, 89, None, 0, 35, 9] When I run this : L = filter(None, L) I get this results [23, 234, 89, 35, 9] But this is not what I need, what I really need is : [0, 23, 234, 89, 0, 35,…
mongotop
  • 7,114
  • 14
  • 51
  • 76
300
votes
6 answers

Python None comparison: should I use "is" or ==?

My editor warns me when I compare my_var == None, but no warning when I use my_var is None. I did a test in the Python shell and determined both are valid syntax, but my editor seems to be saying that my_var is None is preferred. Is this the…
Clay Wardell
  • 14,846
  • 13
  • 44
  • 65
264
votes
2 answers

Python void return type annotation

In python 3.x, it is common to use return type annotation of a function, such as: def foo() -> str: return "bar" What is the correct annotation for the "void" type? I'm considering 3 options: def foo() -> None: not logical IMO, because None…
Tregoreg
  • 18,872
  • 15
  • 48
  • 69
209
votes
12 answers

TypeError: 'NoneType' object is not iterable in Python

What does TypeError: 'NoneType' object is not iterable mean? Example: for row in data: # Gives TypeError! print(row)
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
176
votes
7 answers

Replace None with NaN in pandas dataframe

I have table x: website 0 http://www.google.com/ 1 http://www.yahoo.com 2 None I want to replace python None with pandas NaN. I tried: x.replace(to_replace=None, value=np.nan) But I got: TypeError: 'regex' must be a string or a…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
127
votes
13 answers

What is a 'NoneType' object?

I'm getting this error when I run my python script: TypeError: cannot concatenate 'str' and 'NoneType' objects I'm pretty sure the 'str' means string, but I dont know what a 'NoneType' object is. My script craps out on the second line, I know the…
insecure-IT
  • 2,068
  • 4
  • 18
  • 26
126
votes
5 answers

Python 3 type hinting for None?

def foo( hello: str='world', bar: str=None, another_string_or_None: str|????=None): pass I'm trying to set a type hint in Python in a function, you can add more than one type hint with something: str|bool='default value', but,…
fj123x
  • 6,904
  • 12
  • 46
  • 58
93
votes
5 answers

Why does append() always return None in Python?

list = [1, 2, 3] print(list.append(4)) ## WRONG, print does not work, append() returns None ## RIGHT: list.append(4) print(list) ## [1, 2, 3, 4] I'm learning Python and I'm not sure if this problem is specific to the language and how append is…
starcodex
  • 2,198
  • 4
  • 22
  • 34
81
votes
3 answers

Sort list while pushing None values to the end

I have a homogeneous list of objects with None, but it can contain any type of values. Example: >>> l = [1, 3, 2, 5, 4, None, 7] >>> sorted(l) [None, 1, 2, 3, 4, 5, 7] >>> sorted(l, reverse=True) [7, 5, 4, 3, 2, 1, None] Is there a way without…
Nikolai Golub
  • 3,327
  • 4
  • 31
  • 61
63
votes
9 answers

Python: "TypeError: __str__ returned non-string" but still prints to output?

I have this piece of code which creates a new note. When I try to print, I get the following error even though it prints the output: Error: C:\Python27\Basics\OOP\formytesting>python notebook.py Memo=This is my first memo, Tag=example Traceback…
user1050619
  • 19,822
  • 85
  • 237
  • 413
1
2 3
89 90