#๐ 'None' data type
63 messages ยท Page 1 of 1 (latest)
@trim pilot
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
types.NoneType```
The type of [`None`](https://docs.python.org/3/library/constants.html#None).
Added in version 3\.10\.
- for whatever reason you liked but usually when you don't have stuff to return
e.g. Your function might try to obtain a record from database, and you could return None when no record is found (or you could raise an exception which doesn't need None)
it can also be used as default value for function parameter
- everywhere but ^
It's the default return value of functions, because functions are always required to return a python object of some kind
Can you say which page of which book?
return
simply give None?
yes
!e
return
:x: Your 3.12 eval job has completed with return code 1.
001 | File "/home/main.py", line 1
002 | return
003 | ^^^^^^
004 | SyntaxError: 'return' outside function
!e
def f1(): return
print(f1())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
None
!e
def f1(): ... # Do literally nothing
print(f1())
:white_check_mark: Your 3.12 eval job has completed with return code 0.
None
ok got it
can we use at any other place in the code
:warning: Your 3.12 eval job has completed with return code 0.
[No output]
see, you used it :)
here?
?
Yes, you can assign it to a variable just like any other value. It's usually used to say "no value was given"
new = input('Enter a number')
print(new)```
it is printing none if i enter none..
i doubt that there should be no value printed by it
That's the str 'None'
Which looks like None when you print it
!e
print('None')
print(None)
๐
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | None
002 | None
both?
They look the same if you print them, something to watch out for
ok
This is to do with the __repr__ and __str__ methods
A bit complicated, but important to get right if you use them:
https://docs.python.org/3/reference/datamodel.html#object.__repr__
https://docs.python.org/3/reference/datamodel.html#object.__str__
I think this is the most recent time I've used None: https://github.com/agronholm/anyio/commit/3a62738be0ba8d7934bd447e3833d644d414d49a#diff-bca1f1fd66614ba723b3440cc1656412e65106e3130c5ff68cbae1d15d9bf08bR189
what is this code for
github is what i am still learning to operate
i connected the github with vs code..
but i dont use it much..
it was so hectic to make it work
__str__ means "this is how you convert to a string", so str(None) == "None". __repr__ means "this is how I want something to be represented". They both give you a string, but they have a difference in meaning. I don't think it's important for you at this stage.
Wow. This much text on None. None represents lack of value. For example a function that doesn't return anything in fact still returns None. Some other other languages might have null or nullptr or something else too, but Python has None.
This code is to check if an address is a domain name or an IP address. It tries to parse the address as a IP address and if it fails it assigns None instead. The code then processes the IP address or the None object differently
This is done because getaddrinfo is slow and expensive, and we want to skip it for IP addresses where it's redundant work
So it's using None here to represent an error case
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.