Every Python Dev Should Know THIS Concept

preview_player
Показать описание
Today I'm going to be sharing with you a very important concept that you should know in Python regarding the primitive data type constructors.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

So as a summary, you have to know that Python doesn't default to uninitialized objects (i.e. null objects or undefined objects), but gives you some default when not present (when unspecified)

antoniong
Автор

These classes return objects containing their respective default values:
int: 0
float: 0.0
str: ""
bool: False
list: []
tuple: ()
dict: {}
set: set()

Thank you 😊

dipeshsamrawat
Автор

Why are people confused by this behavior? If you have ever worked with any other high level programming language like Java then you know what a default initialization is. This is not a new or special concept.

Finkelfunk
Автор

I guess what’s confusing is that existence of a feature tends to imply that it has a use case, because why would a feature exist if it wasn’t useful? Yet, calling int() has… no possible use case, as far as I can imagine. However, set() is useful, so it doesn’t seem as weird.

JordanMetroidManiac
Автор

Thanks for the video!

But this explanation works for me only for the “complexe” datatypes (str, list, tuple, dict, set, …). Because you create an object of a certain type that is empty (i.e. no values assigned).

But for primitive datatypes we get a certain default value?! I’m not sure if there is such thing as an empty int, bool or float in Python. You could argue that there is the None value. But None is actually a special own type in Python. So it seems to me it’s just a workaround to set a default value for primitive datatypes.

pianocrasher
Автор

I know it's very self-explanatory, but frozenset, bytes, bytearray, Decimal, Fraction, OrderedDict, UserDict, the numpy types, and many more from the standard library and external libraries also have this behavior.

largewallofbeans
Автор

I'm shocked that people are shocked at this behavior. I've used the empty set constructor many times, heck, I've even created a custom list class, which I even accounted for the empty arguments. It just makes complete sense that int() would return 0, even though I've never seen that before.

ego-lay_atman-bay
Автор

1:01 it makes sense, just like a type of a class should be None, since no instance is constructed and that’s how you create a singleton class

eduferreyraok
Автор

Sum of integers:
s = int()
for v in [1, 2, 3]:
s += v

Concatenation of strings:
s = str()
for v in [“1”, “2”, “3”]:
s += v

Concatenation of lists:
s = list()
for v in [[1, 2], [3, 4], [5, 6]]:
s += v

JordanMetroidManiac
Автор

Just another quirk of Python’s syntax. Functions and class constructors are called in the exact same way.

If the syntax were adjusted to show the difference, such as having a specific convention such as Class.new() -> Class static method (or similar) could potentially resolve this confusion for beginners and make OOP code in Python significantly clearer.

MrCreepsGaming
Автор

Good video. I'm studying English and I like your accent. I would like to know where is your accent. Please. Thank a lot. 😊👍

exkalybur_dev
Автор

I've the type Matrix from Algebra :)

juancarlospizarromendez
Автор

me as a c programmer says easy to understand. just a pointer which was allocated memory space but never got a value

mgmn
Автор

It should throw a f error for primitive types! Just say 0, False, etc… (string constructor and other like set and dict are great

Why would you allow int()? For what? bool()? Unnecessary complexing your code with default values is insane

МихайлоДвалі