Slots make Python FASTER and LEANER

  Рет қаралды 22,094

Carberra

Carberra

Күн бұрын

Пікірлер: 44
@Chalisque
@Chalisque 2 жыл бұрын
Something else about the __slots__ attribute is that it is immutable and is shared between all instances of the class. The following illustrates class A: def __init__(self,x,y): self.x = x self.y = y class B: __slots__ = ("x","y") def __init__(self,x,y): self.x = x self.y = y a = A(1,2) b = B(1,2) a1 = A(1,2) b1 = B(1,2) print(a.__sizeof__()) # 32 print(b.__sizeof__()) # 32 print(a.__dict__.__sizeof__()) # 88 print(b.__slots__.__sizeof__()) # 40 print(a.__dict__ is a1.__dict__) # False print(b.__slots__ is b1.__slots__) # True
@Carberra
@Carberra 2 жыл бұрын
Oh nice, I was actually unaware of both these points! Even better than I thought lmao. One additional point on top of that is that child classes inherit slots from parent classes, and can be extended by defining a new set of slots.
@Pedro_lopes22
@Pedro_lopes22 Жыл бұрын
You should research before doing a video. That video explains almost nothing about slots.
@SillyLittleMe
@SillyLittleMe 2 жыл бұрын
This is the first time i am hearing about this, but it is such an important concept! Good video, keep it up!
@Carberra
@Carberra 2 жыл бұрын
Thanks man! Yeah it's omega useful.
@SeriouslyTechy
@SeriouslyTechy 2 жыл бұрын
Awesome video man, for some one thats been working with python for 4 years, this was news to me. Also, explained amazingly, no fillers, no wastage of time. Definitely subscribing for amazing future content
@Carberra
@Carberra 2 жыл бұрын
I appreciate that man, glad you enjoyed it! Yeah it was a good few years before I learned what they were as well, maybe about the 4-year mark as well actually.
@Kilogya
@Kilogya 2 жыл бұрын
I'm half a year using python and I'm glad to hear about this. I'm subscribing.
@Carberra
@Carberra 2 жыл бұрын
Appreciate the support my guy!
@zalmanbelinow7139
@zalmanbelinow7139 2 жыл бұрын
Cool video!
@KeyserTheRedBeard
@KeyserTheRedBeard 2 жыл бұрын
neat video Carberra. I killed the thumbs up on your video. Continue to keep up the solid work.
@Carberra
@Carberra 2 жыл бұрын
One murder = one happy boi. Thanks! (:
@biskitpagla
@biskitpagla Жыл бұрын
How does this work wrt type hints?
@spatialnasir
@spatialnasir 2 жыл бұрын
Never heard of it but, thanks to your video, it's now done and dusted.
@Carberra
@Carberra 2 жыл бұрын
Glad I could help out (:
@chalkypj
@chalkypj 2 жыл бұрын
Thank you for this, really helped my understanding
@Carberra
@Carberra 2 жыл бұрын
You're welcome! Glad it helped (:
@karixening
@karixening 2 жыл бұрын
That is cool. I'd kind of like it if the behavior was flipped, where the slots style was enabled by default, and you would have to explicitly enable the extensible dict functionality 🤔. Cause I consider doing that kind of a bad practice. Not sure how that would work though, unless the slots defaulted to whatever props you assigned the class in the constructor.
@Carberra
@Carberra 2 жыл бұрын
Slots is pretty new, so would require a breaking change to swap it round. Makes me wonder if will be the default in Python 4 though -- there are a lot of things Python 3 added that are optional, but should be defacto.
@robertsjoblom9279
@robertsjoblom9279 2 жыл бұрын
How's it work with inheritance?
@Carberra
@Carberra 2 жыл бұрын
Slots are inherited automatically. To extend the slots for the child class, you create a new slots, and Python uses them both to determine what it can and can't use.
@johanohlin9945
@johanohlin9945 2 жыл бұрын
Very helpful!
@fusionsid270
@fusionsid270 2 жыл бұрын
yo thanks I didn't know about this before
@0xtz_
@0xtz_ 2 жыл бұрын
Very interesting hmm we need more
@NachoooX0998
@NachoooX0998 2 жыл бұрын
Loved it, slay sis
@yato3335
@yato3335 2 жыл бұрын
When you use timeit, you can pass definitions used in the code as "globals" parameter.
@Carberra
@Carberra 2 жыл бұрын
Would you pass the name of the function or the function itself?
@yato3335
@yato3335 2 жыл бұрын
@@Carberra Something like this timeit(code, globals={"Profile": Profile})
@Carberra
@Carberra 2 жыл бұрын
Oh okay cool, thanks for the tip!
@monsieuralexandergulbu3678
@monsieuralexandergulbu3678 2 жыл бұрын
Remember kids, profile first
@MangoNutella
@MangoNutella 2 жыл бұрын
"mail" haha
@Carberra
@Carberra 2 жыл бұрын
Hm?
@MangoNutella
@MangoNutella 2 жыл бұрын
@@Carberra You wanted to type "male" but did "mail" instead two times haha
@Carberra
@Carberra 2 жыл бұрын
Oh right lmaoooo. I am letter.
@re.liable
@re.liable 2 жыл бұрын
Anyone know why it is named "slots"? I wish it was called "attributes" or something more intuitive haha
@Carberra
@Carberra 2 жыл бұрын
That's actually a very good question. I can't seem to find any official reasoning, perhaps other than that other names were considered too long (though that wouldn't discount "attrs" being used, so I'm not sure).
@DBZM1k3
@DBZM1k3 2 жыл бұрын
I assume its supposed to be a slot in memory that is accessible.
@timstevens3361
@timstevens3361 2 жыл бұрын
faster n leaner like C ?
@Carberra
@Carberra 2 жыл бұрын
No haha. For that you'd need something more sinister such as Cython (doing a stream on that tomorrow *cough plug cough*) or PyPy.
@etchris
@etchris 2 жыл бұрын
Ethan . . . 23 . . . uhhhhhh that's me
@Carberra
@Carberra 2 жыл бұрын
Dude wut, that's spooky 👀
@helish_88
@helish_88 2 жыл бұрын
you're "mail"
@Carberra
@Carberra 2 жыл бұрын
Looks like I'm the only person that didn't notice this lmao.
@overloader7900
@overloader7900 2 жыл бұрын
Now we just need to add an ability to force types, and oh wait thats just c++ with a bunch of libraries
You NEED to know about dataclasses in Python
8:57
Carberra
Рет қаралды 4,7 М.
Cython makes Python INSANELY FAST
19:08
Carberra
Рет қаралды 36 М.
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Python GC Settings - Change This and Go 20% Faster!
14:14
Michael Kennedy
Рет қаралды 6 М.
What are __slots__ in Python? Optimize Your Code!
10:07
NeuralNine
Рет қаралды 11 М.
Nox is the ONLY package you'll EVER need
15:55
Carberra
Рет қаралды 3,8 М.
Python __slots__ and object layout explained
10:16
mCoding
Рет қаралды 94 М.
Make Python code 1000x Faster with Numba
20:33
Jack of Some
Рет қаралды 448 М.
A Simple & Effective Way To Improve Python Class Performance
12:40
super/MRO, Python's most misunderstood feature.
21:07
mCoding
Рет қаралды 223 М.
Data Classes in Python Are The New Standard
20:34
NeuralNine
Рет қаралды 68 М.
Is Python ACTUALLY faster without the GIL?
13:48
Carberra
Рет қаралды 2,5 М.
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН