What Are Double Colons Used For in Python?

  Рет қаралды 4,572

NeuralNine

NeuralNine

Күн бұрын

Пікірлер: 16
@dabkolay
@dabkolay 16 сағат бұрын
i thought I knew basically most of py, but still fiding new concepts, thank you!!
@xzex2609
@xzex2609 10 сағат бұрын
I would rater that you talk about the slice object it selt s = slice(start,stop, step) and the magic method that let you define this behavior on other iterable data types or collections. but it was cool
@parasgaur1856
@parasgaur1856 22 сағат бұрын
Is there any difference between mylist[::] and mylist? Like I know the result will be the same. But does these [::] return a different class/sub-class of it? Or is there anything else, like for example some very niche case where one would prefer using [::] instead of just simply the name?
@thomasgoodwin2648
@thomasgoodwin2648 22 сағат бұрын
As I understand it, it will be the same class, but mylist returns a direct reference to the object, while mylist[::] returns a new object that is a copy of the original. I have seen it used to enforce the return of copies of lists, rather than references to the original list as a matter of security. (return by value vs by reference).
@david-komi8
@david-komi8 20 сағат бұрын
In some rare cases you would want to use mylist[::]. In summary, that instruction makes a shallow copy of the original list named mylist, so this all has to do with copies. If you define mylist, for example, like this: mylist = list(range(1, 11)) # this creates the list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] And let's say you want a copy of that list, modify the copy but not the original, your first instinct might be to write something like: list_copy = mylist.copy() This creates something called a shallow copy, where, in some cases, changing one of the lists might change the other list, here's an example: mylist = [1, [1, 2], 3] list_copy = my_list.copy() list_copy[1][0] = 7 # here, the copy is modified print(mylist) # -> [1, [7, 2], 3] suddenly, the original changed too This is because the objects are duplicated but their inner objects are referenced instead of duplicated, this commonly happens with collections. mylist[::] is the old-school low-key inefficient and whacky way to do a shallow copy for lists and it's only useful for compatibility. To prevent the behavior mentioned before, you have to do a deep copy with the copy.deepcopy function from the copy module. In terms of instances, mylist[::] returns the exact same class as mylist, a list. So, they are different in some way, because modifying the top elements of the list doesn't affect the other list, but if you are working with, for example, nested lists, modifying one list could modify the other. It is okay to use a shallow copy if you're completely sure that the list does not contain collections or objects that have inner objects.
@thomasgoodwin2648
@thomasgoodwin2648 19 сағат бұрын
@@david-komi8 What he said!☝ Thanks for breaking it down right. 😉
@daftvader1505
@daftvader1505 17 сағат бұрын
list = [1, 2, 3] mylist = list mylist[0] = 100 print(list) # Output: [100, 2, 3] Vs list = [1, 2, 3] mylist = list[::] mylist[0] = 100 print(list) # Output: [1, 2, 3] print(mylist) # Output: [100, 2, 3]
@Stefch2
@Stefch2 21 сағат бұрын
Which playlists is it part?
@dipeshsamrawat7957
@dipeshsamrawat7957 3 сағат бұрын
Thank you 😊
@denisevp9431
@denisevp9431 18 сағат бұрын
very helpful guide, thanks
@efersondenis2782
@efersondenis2782 22 сағат бұрын
Mano tá funcionando a dublagem 😂😂😂😂🎉🎉🎉🎉 parabéns ao Google... Tomara que todos os vídeos funcione com essa tecnologia ❤❤❤❤❤
@lnstagrarm
@lnstagrarm 20 сағат бұрын
Wow i didn’t know about double colon
@afkDog
@afkDog 11 сағат бұрын
Thanks!
@AlexandreRousselet
@AlexandreRousselet 19 сағат бұрын
And one more need useful thing!!! Thanks🎉
@bobby_ridge
@bobby_ridge 41 минут бұрын
ILY, dude
@daftvader1505
@daftvader1505 17 сағат бұрын
I didn't know about this... But I simply asked chat GPT... 30 seconds to an answer. Moral of the story don't make videos on really simple things
5 Custom Python Decorators For Your Projects
25:40
NeuralNine
Рет қаралды 10 М.
When Should You Use Generators in Python?
10:01
NeuralNine
Рет қаралды 6 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Chat with PDFs: RAG with LangChain, GPT & LLaMa in Python
22:55
NeuralNine
Рет қаралды 3,1 М.
Stock Watchlist in Python - Finance Project For Beginners
29:21
NeuralNine
Рет қаралды 3,6 М.
Python Graph Coloring Algorithm
2:37
DOHArdthings
Рет қаралды 182
Marimo: Code Web Applications with Python Notebooks
12:58
NeuralNine
Рет қаралды 5 М.
Define the winning strategy for white and it will lead you to victory!
6:28
Evaluation Metrics For Regression - When & Why To Use What
19:09
NeuralNine
Рет қаралды 2,4 М.
Tkinter Designer: Modern GUIs with Drag & Drop
9:43
NeuralNine
Рет қаралды 9 М.
nice olympiad mathematics || find value a and b ||
15:57
Sahaj Maths Study
Рет қаралды 3
Самый лучший телефон
0:58
Hi Store Media
Рет қаралды 413 М.
PS5 + PS5 = ⁉️
0:22
Gleb POV
Рет қаралды 3,1 МЛН
Mac Mini M4 - ОН ИЗМЕНИТ ИГРУ
17:47
ЗЕ МАККЕРС
Рет қаралды 79 М.