#๐Ÿ”’ 'None' data type

63 messages ยท Page 1 of 1 (latest)

trim pilot
#

From the book, it confuses me...can someone help me understand

  1. What none type is?
  2. why is it used?
  3. where can i use?
honest mangoBOT
#

@trim pilot

Python help channel opened

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.

zenith shard
#
  1. NoneType
#

!d types.NoneType

honest mangoBOT
#

types.NoneType```
The type of [`None`](https://docs.python.org/3/library/constants.html#None).

Added in version 3\.10\.
zenith shard
#
  1. 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

#
  1. everywhere but ^
bleak plover
bleak plover
trim pilot
#

return

simply give None?

zenith shard
#

yes

bleak plover
#

!e

return
honest mangoBOT
# bleak plover !e ```python 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
zenith shard
honest mangoBOT
zenith shard
#

have to use it in a function :)

#

and there are something call implicit return

trim pilot
#

i am sending

zenith shard
#

!e

def f1(): ... # Do literally nothing
print(f1())
honest mangoBOT
trim pilot
#

can we use at any other place in the code

zenith shard
#

returnnope, it have to be in a function

Noneyes, everywhere

#

just

#

!e

None
honest mangoBOT
zenith shard
#

see, you used it :)

trim pilot
zenith shard
bleak plover
#

Use a code block and !e

#

!code

honest mangoBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

tribal gate
trim pilot
#
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
bleak plover
#

That's the str 'None'

#

Which looks like None when you print it

#

!e

print('None')
print(None)
trim pilot
#

๐Ÿ‘

honest mangoBOT
trim pilot
#

both?

bleak plover
#

They look the same if you print them, something to watch out for

trim pilot
#

ok

bleak plover
#

This is to do with the __repr__ and __str__ methods

trim pilot
#

what are these?

#

nvm lets code

bleak plover
trim pilot
#

not to learn this now

#

can you give me more examples

trim pilot
#

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

tribal gate
#

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

rare heron
#

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.

bleak plover
# trim pilot what is this code for

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

honest mangoBOT
#
Python help channel closed

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.