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
@Carberra2 жыл бұрын
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 Жыл бұрын
You should research before doing a video. That video explains almost nothing about slots.
@SillyLittleMe2 жыл бұрын
This is the first time i am hearing about this, but it is such an important concept! Good video, keep it up!
@Carberra2 жыл бұрын
Thanks man! Yeah it's omega useful.
@SeriouslyTechy2 жыл бұрын
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
@Carberra2 жыл бұрын
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.
@Kilogya2 жыл бұрын
I'm half a year using python and I'm glad to hear about this. I'm subscribing.
@Carberra2 жыл бұрын
Appreciate the support my guy!
@zalmanbelinow71392 жыл бұрын
Cool video!
@KeyserTheRedBeard2 жыл бұрын
neat video Carberra. I killed the thumbs up on your video. Continue to keep up the solid work.
@Carberra2 жыл бұрын
One murder = one happy boi. Thanks! (:
@biskitpagla Жыл бұрын
How does this work wrt type hints?
@spatialnasir2 жыл бұрын
Never heard of it but, thanks to your video, it's now done and dusted.
@Carberra2 жыл бұрын
Glad I could help out (:
@chalkypj2 жыл бұрын
Thank you for this, really helped my understanding
@Carberra2 жыл бұрын
You're welcome! Glad it helped (:
@karixening2 жыл бұрын
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.
@Carberra2 жыл бұрын
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.
@robertsjoblom92792 жыл бұрын
How's it work with inheritance?
@Carberra2 жыл бұрын
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.
@johanohlin99452 жыл бұрын
Very helpful!
@fusionsid2702 жыл бұрын
yo thanks I didn't know about this before
@0xtz_2 жыл бұрын
Very interesting hmm we need more
@NachoooX09982 жыл бұрын
Loved it, slay sis
@yato33352 жыл бұрын
When you use timeit, you can pass definitions used in the code as "globals" parameter.
@Carberra2 жыл бұрын
Would you pass the name of the function or the function itself?
@yato33352 жыл бұрын
@@Carberra Something like this timeit(code, globals={"Profile": Profile})
@Carberra2 жыл бұрын
Oh okay cool, thanks for the tip!
@monsieuralexandergulbu36782 жыл бұрын
Remember kids, profile first
@MangoNutella2 жыл бұрын
"mail" haha
@Carberra2 жыл бұрын
Hm?
@MangoNutella2 жыл бұрын
@@Carberra You wanted to type "male" but did "mail" instead two times haha
@Carberra2 жыл бұрын
Oh right lmaoooo. I am letter.
@re.liable2 жыл бұрын
Anyone know why it is named "slots"? I wish it was called "attributes" or something more intuitive haha
@Carberra2 жыл бұрын
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).
@DBZM1k32 жыл бұрын
I assume its supposed to be a slot in memory that is accessible.
@timstevens33612 жыл бұрын
faster n leaner like C ?
@Carberra2 жыл бұрын
No haha. For that you'd need something more sinister such as Cython (doing a stream on that tomorrow *cough plug cough*) or PyPy.
@etchris2 жыл бұрын
Ethan . . . 23 . . . uhhhhhh that's me
@Carberra2 жыл бұрын
Dude wut, that's spooky 👀
@helish_882 жыл бұрын
you're "mail"
@Carberra2 жыл бұрын
Looks like I'm the only person that didn't notice this lmao.
@overloader79002 жыл бұрын
Now we just need to add an ability to force types, and oh wait thats just c++ with a bunch of libraries