Chippada delivers a concise, production-focused breakdown of Python attribute management that moves beyond basic syntax. The inclusion of `__slots__` is a sharp touch for teaching robust, bug-resistant coding practices.
深掘り
前提条件
- データがありません。
次のステップ
- データがありません。
深掘り
Python OOPs Part2追加:
Hello guys, welcome back to the channel.
So e video we will talk about the oops concepts in Python part two which are class variables.
Previous video I will put that link in the description below and also this place. So please go ahead and watch that video and come back. So e video we will learn about class variables but class variables I wanted to explain a common situation where a lot of developers will do the mistakes while doing the classes.
Mistake you will not face it when you are working alone. This mistake you will face it when you are working in a team and team collaboration.
I will tell you how this mistake will happen and I will also tell you how to avoid this mistake. So this is something that I am sharing from my personal experience. Last video we created this class which is basically a employee class then local we have the init method and it takes three variables first last and pay and we know what is self and everything and we derived an email from the first lower and class lower and pay and we have two different methods which is basically a full name and the increase pay and increase pay function low I made a small change where I put the hardcode value of 1.04 04 last class we put the value as percent but I removed that and I I will tell you why in some time so let's say employee class and you created an instance of that class in the employee one by passing the values rahul sharma 80,000 8 lakhs now when you say print employee 1dict it is going to give you what are the details the first name last name He wants to update the salary of this employee one instance. So he want to update it pay equal to 9A but he made a typo and put it as pya example. So usually VS code is showing the issue but it will not throw issue when you execute the code. So let's say I will execute this code.
The pay increase it still has the 8 lakh rupees but it is not throwing any error.
But what if I print this employee one dict after this one and let's execute this and look at this one initial we have the Rahul the last name as Sharma and pay 8 and email but it didn't update the pay value because it didn't recognize what is this spelling py n and it added a new attribute to this employee one instance with the new typeo that is created py and jp So, Python inherently will not throw any errors on this one because Python thinks that we are all adults and we will not make any mistakes and ma. So when you create a new attribute that is not present in the instance when you created this class then what Python will do is it will create that attribute to this particular instance as an additional attribute and you can see that this is coming at the last one here it's basically a typo but the pay is still 8 lak rupees but not the latest one okay that is what it is happening So but Python happily created a new attribute and your code runs properly and you not find any issues and these are the kind of bugs that are nightmare to debug for developers and in so maybe you created a list of employees and list you have all the instance of all these employees maybe Rahul Sharma Priya Patel Gani and then if I go to the first uh one which which is basically the attri which is basically the instance for the Rahul Sharma and if I put something called as department I am adding a new attribute again and then I will put engineering what if I iterate through all of these employees for employee in employees uh print employee department.
So when you execute this piece of code, so here what is happening initially when you say this is this is my employee one instance and this is my employee 2 instance and you are iterating through all of these instances but employee one instance key you create a new attribute department but employee 2 key department because you didn't have the department as an attribute in your init function.
So when you are iterating through all of your employees and when you are printing the department but second it will throw an error because that attribute is not present when you defined the init. So when you are working in a team actually so how to fix this there is something which is basically called as slots and mata. So what are the slots? So when you create this uh class at the start slots and here equal to you provide the list of attributes that this class can accept.
Let's say first and then I will put last and then I will put pay. So what we are saying we are enforcing the class such that instance create you can only have these attributes but not any other attributes. So let's try all of this right now and let's do this.
Same code we are going to execute again but after adding slots and mata and if you execute this now look at the output say attribute error employee object does not have this attribute under initial when we don't have the slots we didn't get any error and it updates the dictionary of that employee one instance but right now if you don't want to make those kind of mistakes it's always recommended to use this kind of slots uh which is enforcing the python to not create more attributes which are not necessary. So this is enforcing Python.
So important this is what makes or breaks your production code actually. And then we will now talk about an important concept which are called class variables. If you look at this code law when we say increase I did a hardcode value of 1.04 04 NJP and by default whenever I call this function I'm going to increase the salary by 4%.
But this is applicable for every employee because you are doing it for every single time when you call this function but what if company decided to not put only 4% but only 7% flat and wherever you put 1.04 I need this 1.07 07 JL it is very cumbersome and tvata sometimes you want to give specific raise to specific employees maybe employee one gets 10% employee 2 gets 15% and remaining everyone gets 5%. So hard is very bad option. So that is where you will use something called as class variable and you define that class variable just after the class definition. You will not put that inside any methods. You will just put it something like this. Let's say I want to raise percentage. Raise percentage equal to 1.04.
So this is a variable that we are defining inside a class and that is why we call it as a class variable. So then meaning this class variable will be applicable to all of these instances any employee instance create employee instance you can access this pattern. So now you created this class variable and now 1.04 04 hard value we will change it with the class variable which is basically raise percentage and VS code is throwing an warning because percent I need to put self dot raise percentage because everything goes in the self I will talk about some differences why it is self and why not a class we will talk about that so don't worry so now let's create uh two instance of these employees and then let's do this here and I will create instance one which is basically Rahul Sharma and 8 lakhs and then Priya Patel and 9 lakhs and then one of the thing is you can access this class variable using something by the class name employee dot raise percentage we are what we are doing we have this class which is our employee class you are accessing this class variable using the class name dot the variable and you will get 1.04 you are using self and whenever you are creating an instance of a class and employee one an will be acting as a self and whenever you are creating a second instance employee two an will be the self. So that means if you want to access the specific employee raise percentage you can do employee 1.ra percentage and employee 2.
So by default value I will get the output as 1.04. Now let's execute the code and see the output 1.04 1.04 and 1.04 I can apply this method. Let's say increase PJP. So what I will do is uh I will say employee 1 dot raise percentage and I will put uh this one and increase pay not raise percentage increase pay increase pay and then I can just print uh employee 1.ay and if I execute this it is going to create initial 82,000 and it is working perfectly fine we will go one step deeper so class variable classable we created the instance of this employee by using employee one and employee two but before doing anything we are just printing the attribute when you execute this first Rahul Sharma and pay and email last and then but if you look at this percent but when I access print employee one employee 1 dot raise percentage let's execute this I'm getting 1.04 in duku why is it happening so when you create a class variable this class variable is going to get applicable for every single thing so what if I will print something which is employee dot employee dot uh dict So now let's look at all the attributes that this class has name flash to pay and raise percentage this dict has the raise percentage 1.04 and class.
So employ how is this Python finding this employee one percentage as 1.04 04.
So the issue is when you say the first when you write employee 1.
_ dict has this variable which is raise percentage percent employ which is basically employee dot dict. So yes then you will access that value directly.
It will throw an attribute error.
Now the second topic which is how to override this class variables. So by default rise 1.04 but what if I want to change this value to maybe 1.07. So I can directly change from the class value itself. I can say employee dot raise percentage equal to 1.07.
And if I print employee raise percentage, this is going to change uh it to 1.07. But at the same time what if we will do employee 1 dot raise percentage and then we will also print employee 2 dot raise percentage and all of these will become 1.07 by default in the we are changing at the root level which is basically employee raise percentage is 1.07 so can I change from the individual level yes so now everything is set to 1.07 07 I will put employee 1 dot raise percentage one raise percentage and I will put it as 1.15 then meaning I am changing the raise percentage for a specific employee instance but not every single thing and then let me print all of this here now let's try to execute this now look at it by default class 1.07 07 and this is for 1.15 only for a specific instance of employee 1 that we did here employee 1% equal to 1.15 and then the last class instance which is employee 2 it is going to stay and you might get one more doubt sir employee access so can I just say instead of self can I do employee dot raise percentage yes you can absolutely do this but you are always accessing this global variable, not the self instance variable. Self will always replace with the instance that you created during the class creation. Employ By default you are only accessing this value which is default class variable of this particular class. So I usually never use this because I have the freedom to change the individual classes and also the freedom to change the global level class variable as well. So I prefer to keep it like this but if you want you can keep it as a employee dot raise percentage as well. And maybe let's take one more example which is let's say employees create and you want to count how many employees are present in your company. So number of employees and a concept it is applying on a company basis not on a employee basis.
So I will create one more variable which will say count equal to zero and then whenever we are creating an instance of this class by default this is going to get executed. So whenever I create an employee, I will increase my count plus equal to 1. So prisari when I create uh this employee instance it is going to increase my count but I cannot directly access count. I will say employee dot employee dot count and if I do this and if I print after all of these things let's say uh print uh count print employee count employee dot count and if I execute this it will give me an output of two so when I created the instance for the first time so let's say so when I call this for the first time it goes here and value zero and and this will get initialized and pass and it will create the email and here you are updating the count as + one. So initially when you created for the employee one this value became + one and when you executed this line the second line this value is already + one and when you execute this definition again this + one is going to add one more + one this will become two and this count will become two and that is what you are printing here and this will give me the output as two. That is how the global variable works and think about logically.
It is not that I employ one account separate two account separate. That is why it is always beneficial to use that as a global class variable rather than individual self count.
I can technically do self count but that doesn't make any sense. Let me show you an example as well.
We will create one more attribute which is like self count. This is a very interesting use case. Let's see how it happens.
Whenever you are writing self, self is always this employee one, employee two because it is not from a class. Self will always be replaced by the instance that you are calling for. So it could be employee one or employee two. But we have the global value which is called as count as well. Employee count.
Let's print employee.
Employee count. And then after this I will also print employee 1 dot count. So what is happening here? Let's execute this. And the output end 1 comma 2. So let's look at what is the output. Here the output is one and here the output is two. Why it is 1 and two. So this is a very interesting use case guys. So let's understand what is happening. So initial when you executed employee Rahul Sharma and 8 lakhs and JP count value zero and this will get executed and the initial value of count is zero and you are adding + one so this will become + one okay you are looking selfount so self do count so first Python will look into employee one dot _ it will update that then it will fall back to this count which is the class count. So it's already plus one plus one value you are making plus one and that will become two and that is why employee one count is becoming two later on. So it is little bit confusing. So it's better not to use this self count if it is a global operation that every employee or every instance can have on.
So these are all the common mistakes that people usually do when creating the class variables. So summary intent whenever you have a variable that is applicable to all those are called class variables and you are going to define all of these the moment you define your class.
If there are any variables or attributes that are specific to a particular instance, these are called instance variables and each instance has its own copy. But these class variables cannot have multiple copies. So how to decide which one to use? First question whether create a variable is it going to be different for every instance or is it going to same for every instance? If it is going to be same for every instance use class variables. If it is not then use instance variables. So now we will quickly jump back to our regime builder code. If you look at this class, this class is basically called as seniority level. Then la we declared all of these which are class variables. Student class initialization we have all of these values and then we have something which are normal instance variables as well.
But later on if you look at this one which is pipeline step even these are all the class variables. So now you are understanding what is happening in this code step by step. We still have something to understand what is this thing enam and in the class inheritance and in all of those things and we will learn all of those concepts in the next video. So stay tuned for the next one which is basically class inheritance.
And if you are enjoying the content please subscribe and if you know someone who wants to learn all of these concepts in detail please share it with them. And if you are learning something new, please also comment.
関連おすすめ
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











