Тёмный

"None" and "NoneType" Explained In Python 

Indently
Подписаться 222 тыс.
Просмотров 10 тыс.
50% 1

Опубликовано:

 

24 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 31   
@acmethunder
@acmethunder 2 дня назад
I’ll now be using the term “nunchuck” when pointing out missing checks for None in PRs.
@max.caimits
@max.caimits 2 дня назад
As I understand this: NoneType is the type of None as it's seen by the runtime. None is the type of None as it's seen by a static type checker.
@FighterAceee94
@FighterAceee94 2 дня назад
I would explicitly use the None check in the form of "if user_selected is not None:", because "if user_selected" will catch multiple other cases, such as user_selected being equal to 0 or False.
@landsgevaer
@landsgevaer 2 дня назад
...or the empty string in his example, indeed.
@michaelhoffmann2891
@michaelhoffmann2891 2 дня назад
At first I was going to mildly gripe that you didn't also deal with NoReturn, but now agree that it would have just distracted in this case. Maybe another video on None vs NoReturn?
@theglobalsouth9680
@theglobalsouth9680 2 дня назад
you can use NoneType() to construct None. if you want to
@ChiliJ
@ChiliJ 2 дня назад
That's a nice way to confuse beginners, which I consider myself to be. But I'll just ask Chatgpt, Claude or GitHub copilot to explain the code to me.
@PsquareDev
@PsquareDev 2 дня назад
Great explanation of None and NoneType in Python! Cleared up so much confusion. 🙌
@evlezzz
@evlezzz День назад
1. You managed to mix up two different things: return type and return value. It's true that if value is not specified in return statement or a function finishes without reaching a return statement, it returns value of None. That fact has nothing to do with "return type" or type annotation that you specify in the function definiton after "->". 2. Default annotation for return type is not None, but Any. That is stated directly in PEP 484 and documentation. 3. There is a weird "exception" for that: Guido managed to push an inconsistent but practical decision that for __init__ method annotation should default for None (google "Allow __init__ with signature but no return type" mypy github ticket for details). Not sure if that is written down anywhere else... 4. Consistent behaviour would be to accept NoneType as valid type annotation and to not accept None, since we are supposed to specify types and not values. But when typehints were designed, somehow adding this specific exception instead of a consistent solution was pushed. Since whole type annotation system is made like that - that looks relatively organic. :-D
@pharoah327
@pharoah327 2 дня назад
To the people saying that since None is a Singleton that it is the same as NoneType, this is utterly and completely incorrect. I've written Singleton classes (a lot of them) in many different languages. I've also written classes with only static members (in C# these are known as static classes, however not all languages call them that). In neither case was the object the same as the class. First, static classes don't have any objects. Second, as a basic object oriented principle, classes and objects are two different things. One comes from the other. Python's interpreter may blur the lines a bit during interpretation but they are in fact separate concepts. Making a Singleton class normally means you grab the Singleton object from the class itself, and can call it whatever you like. You can make that Singleton instance a global and can therefore grab it directly (which is probably what Python is doing), however at no point is this instance equal to the class itself. The fact that Python demands None instead of NoneType in the type annotation is weird and is not in line with literally any other language. My guess is that they do that for ease of use, breaking consistency for readability. Also, you shouldn't be constructing your own None by using NoneType(). I'm not sure if the interpreter does any magic here to save you from pitfalls, but technically this would construct a new object of NoneType which would not be equal to the None object already given to you. Comparisons to None would then fail. Most languages prevent this by making the ctor private, something that you can't do in Python (at least that I'm aware of).
@theEtch
@theEtch 2 дня назад
interesting, thanks
@dragweb7725
@dragweb7725 День назад
in python you can make class variables really private by putting double underscore as name prefix, e.g. "__private_var" is never accessible from outside its class
@jamesford3282
@jamesford3282 2 дня назад
in 4:48 You missed that: print(type(user_selected) is None) just for comparison, that it will show that this will return False to more understand this
@samarth1071
@samarth1071 2 дня назад
was just stuck on a NoneType error caused by pydantic model for almost 15 mins 😭😭
@Jimbooos
@Jimbooos 2 дня назад
so the fact that None is an alias for NoneType and also the name of the only instance doesn't bother me, as the principle of singleton would anyways makes it redundant to call both a different name. However, not being able to explicitly write NoneType when you want is a bit weird... But acceptable for the sake of clarity and simplicity : everything related to None is None. It is just slightly confusing when you try to understand the core of python because it appears to be an exception to the neat typing system.
@evlezzz
@evlezzz День назад
None is not an alias for NoneType.
@dipeshsamrawat7957
@dipeshsamrawat7957 2 дня назад
You are right. `NoneType` is the type or the super class for None objects in Python. ``` val = None print(isinstance(val, None)) ``` The above code will throw an error because it is interpreted as No Value, while the following code would not: ``` from types import NoneType val = None print(isinstance(val, NoneType)) ``` Thank you 😊
@evlezzz
@evlezzz День назад
Super class is not the right term here since None is not a class.
@dipeshsamrawat7957
@dipeshsamrawat7957 День назад
@@evlezzz No. The keyword `None` is actually an object of `NoneType` class. This same goes for `True` and `False` also, these are objects from `int()` as super class.
@evlezzz
@evlezzz День назад
@@dipeshsamrawat7957 Word "super" makes no sense here. Superclass/subclass describes relations between classes (inheritance, basically), not between a class and it's instance.
@evlezzz
@evlezzz День назад
@@dipeshsamrawat7957 Also, None is not a keyword.
@dipeshsamrawat7957
@dipeshsamrawat7957 День назад
@@evlezzz I meant to describe a group, that's why I used "superclass" word. Also, we can't name a variable as None in Python, so while it is an instance or object, it's not wrong to call it a keyword.
@andhika277
@andhika277 2 дня назад
Dont forget to added on playlist
@RanaUniverse
@RanaUniverse 2 дня назад
i just watched python video but not doing anything myself 🙁🙁🙁
@Madhava_P7
@Madhava_P7 2 дня назад
Are you gonna start SWIFT?
@foxypiratecove37350
@foxypiratecove37350 2 дня назад
No
@sumitbiswas164
@sumitbiswas164 2 дня назад
As `None` is a single instance, it should be of type just `None`. It behaves like a static class (class used as an instance). Similar thing I notice in pygame is that we can use `Event` class as well as `EventType` but both type annotations work same. Overriding type for a class is not useful here.
Далее
How To Write Better Functions In Python
14:17
Просмотров 41 тыс.
Learn Python OOP in under 20 Minutes
18:32
Просмотров 77 тыс.
Cool Wrap! My Book is OUT 🥳
00:27
Просмотров 2,4 млн
РОК-СТРИМ без ФАНЕРЫ🤘
3:05:16
Просмотров 1,3 млн
25 nooby Python habits you need to ditch
9:12
Просмотров 1,8 млн
Google Drive hates developers now
23:56
Просмотров 80 тыс.
What does '__init__.py' do in Python?
6:50
Просмотров 53 тыс.
5 Good Python Habits
17:35
Просмотров 576 тыс.
Coding Was HARD Until I Learned These 5 Things...
8:34
Please Master These 10 Python Functions…
22:17
Просмотров 190 тыс.