Тёмный

Python OOP Tutorial (Object Orientated Programming ) - Static Methods and Class Methods 

Tech With Tim
Подписаться 1,6 млн
Просмотров 69 тыс.
50% 1

Опубликовано:

 

30 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 67   
@letslearnabout4934
@letslearnabout4934 5 лет назад
Summary: Methods: Functions within a class that passes an instance of the same class -> def get_name(self): return self.name Class Methods: Functions within a class that passes its own class -> def get_users(cls): return len(cls.users_created) Static Methods: Functions within a class that doesn't use an instance or the own class. Used as a utility methods that doesn't need to interact with instances of the class -> def say_hi(): print("Hi, I'm a static method from User class!")
@isaiah7310
@isaiah7310 4 года назад
Found this while googling more about class methods and it helped me understand. "A class method is a method that is bound to a class rather than its object. It doesn't require creation of a class instance, much like static method. The difference between a static method and a class method is: Static method knows nothing about the class and just deals with the parameters." - on Programiz
@helplearnhome9542
@helplearnhome9542 3 года назад
@@isaiah7310 static just knows about its parameters and it's function. It is not concerned about the whole class. Thank you all!
@kaustubhpandey7444
@kaustubhpandey7444 3 года назад
methods: deal with instance attributes(variables) as parameters class methods: deal with class variables as parameters static methods: just present in class it has its own parameters this is what I understood feel free to correct me
@GamerBoyHarsh.
@GamerBoyHarsh. Год назад
Thanks!!
@Sciencedoneright
@Sciencedoneright 3 года назад
Thank you for making these! I really don't know why, but when I sit down and try it just pops to my head all the gaps in my knowledge. It's almost magical. Thank you.
@tcironbear21
@tcironbear21 4 года назад
For those confused as to why his class variable "dogs" of the class "Dog" keeps returning locations, I think Tim might have made a mistake. Change "self.dogs.append(self)" to "self.dogs.append(name)" He was saving memory locations in the dogs array not values at the dogs array. I was trying to figure out how to make those Dog names print out, and figured out the simplest way to do that was to just change what was stored in the dogs array then to turn the locations into values. Tim is far more experienced than me and might have a better way of doing this. And it was intentional.
@sidharthanm7761
@sidharthanm7761 3 года назад
I think he wanted to store the class object itself so that he can access it like dogs[i].name.
@benogega2543
@benogega2543 2 года назад
__str__()
@symphoniacus
@symphoniacus 5 лет назад
This series definitely deserves much more views!
@afbdreds
@afbdreds 4 года назад
Static Methods in a phrase: 7:24 Class Methods in a phrase: 8:35
@aerorocketdog6249
@aerorocketdog6249 2 года назад
Incredibly well explained, with use cases and all. Thanks for taking the time to do this, great work!
@wongkingshun
@wongkingshun Год назад
Is it cls serve a smiliar function as self or same? when should i use a cls method then?
@aerorocketdog6249
@aerorocketdog6249 Год назад
@@wongkingshun 'cls' is similar to 'self' in concept. The biggest difference is that 'cls' is referring to the class, and 'self' is referring to an object created by the class.
@urkelturkel5723
@urkelturkel5723 4 года назад
When I first learned object orientated programming. I was told that you have one plural class and one singular class. But you have the plural "object" in your singular class, I have no problem with that, I just want to know your thoughts about that. I think that you do a great job with your videos and as a programmer for many years I feel that I don't have to listen to people talking about Python, or other programming languages, as if we don't know anything about programming.
@Anonymous-1011
@Anonymous-1011 2 года назад
Finally I know why they have created @staticmethod and how we are using them all the time.
@avvn9331
@avvn9331 5 лет назад
Thanks for clearing the concepts, about the static and class methods, I found it very useful to understand the concept correctly, you used a good example in this code sir.
@TechWithTim
@TechWithTim 5 лет назад
Thank you!
@brunojander
@brunojander 9 месяцев назад
Excellent video !
@jhonatanmaia5652
@jhonatanmaia5652 3 года назад
I am in love with your videos. I dont know if its how you suppose to use it, but i use cls to call staticmethod from my class without writing my class name, like: cls.somemethod(x) instead of MyClassName.somemethod(x)
@fredericoamigo
@fredericoamigo 2 года назад
Great video as always, however, I would appreciate it if you could explain decorators in more simple terms/explain decorators more in depth.
@francist3979
@francist3979 5 лет назад
Question! Do all my class methods need @Classmethods above them or can one @Classmethod be placed above all the class methods? Loving your videos
@anandhraj1952
@anandhraj1952 Месяц назад
You are absolutely amazing
@anandhraj1952
@anandhraj1952 Месяц назад
Time bro help me with my doubt how can use class methods in my programs
@vaggo9611
@vaggo9611 4 года назад
really nice explanation, thanks Tim
@NikolaNevenov86
@NikolaNevenov86 4 года назад
nice I finally understand those properties.Thanks!
@pugo7925
@pugo7925 5 лет назад
Question: I made a class in a project I'm working on right now. I made a method within my class that should take one of the attributes that initialized and according to their value it decide how to work. for example: 1. class One_Or_Zero(object): 2. def __init__(self, num): 3. self.num = num 4. 5. def process(self) 6. if self.num = True: 7. return 1 8. else: 9. return 0 10. 11. 12. yoyo = One_Or_Zero(True) 13. print(yoyo.process()) -------------------output-------------------------------------- 1 Now Pycharm suggest me to convert the process method to property. If I do, PyCharm adds @property above the method. I wonder what's meaning Thanks.
@flyingmadpakke
@flyingmadpakke 4 года назад
I don't know much about python, but in other languages like C#, properties can be used to create all kinds of behavior when you change the value or get the value of your attributes. It can also increase visibility/tidy up your code. Going of his example with the Dog class: The class has an instance attribute called name he could access like: tim = dog('tim') tim.name = 'other name' - this would change the name. However you could set the restriction that a valid name must be at least 3 characters long. It could be done with a method, but it could also be done with a property. @property def Name(self): return self.name @Name.setter def Name(self, value): if len(value)>=3: self.name = value else: print('the name must be at least 3 characters long') If Name was a normal method, you would have to do something like: tim.Name('other name') But because it's a property it allows us to use Name kinda as if it was an attribute: tim.Name = 'other name' - tim.name changes. tim.Name = 'AB' - tim.name doesn't change and instead the "error" message is printed to the screen. The reason it prompts you to change into a property is probably because of readability. You have defined a method that doesn't take any parameters and just returns some value. That is exactly what the get part of a property would do. If you made process a property you could just write: print(yoyo.process) - no parenthesis required.
@aminuabdusalam3086
@aminuabdusalam3086 4 года назад
@@flyingmadpakke Thanks for taking time to answer the question.
@uliliulili
@uliliulili 2 года назад
Fucking way more easier with this guy. U rock man
@sleepymarauder4178
@sleepymarauder4178 5 лет назад
Great series. Problem with Windows activation? Join Linux :)
@Sciencedoneright
@Sciencedoneright 3 года назад
THANK YOU SO MUCH!
@MPaz-fh4tx
@MPaz-fh4tx 5 лет назад
You are a great teacher! Congratulations and Thank you!
@gathikjindal3934
@gathikjindal3934 4 года назад
could you please explain the decorators.
@hamzakhalid9428
@hamzakhalid9428 4 года назад
excellent video!
@en8042
@en8042 4 года назад
Why do you have to use the @staticmethod decorator? As I noticed it doesn't make a differnce if you omit it. Also with the classmethod, you can just leave out the decorator, remove the 'cls' from the args, and replace 'cls' with 'Dog' when returning. What I noticed in this case is we can't call these methods on instances. Is it the reason why we use decorators?
@MatrixAbuz17
@MatrixAbuz17 11 месяцев назад
Personally use it to distinguish non generic functions. Although static methods aren’t needed technically for classes but it’ll at least give more meaning to the functions representation
@Sciencedoneright
@Sciencedoneright 3 года назад
You didn't active windows?
@Sciencedoneright
@Sciencedoneright 3 года назад
Your Keyboard just gives the dopamine
@wongkingshun
@wongkingshun Год назад
For static method, why for _ in range (n), what do the underscore here stands for? And why is it use?
@victormanuel8767
@victormanuel8767 Год назад
It is not actually relevant to the topic here. I guess he just wanted to print bark several times
@tcironbear21
@tcironbear21 4 года назад
Do you have to use decorators at all?
@prabhat_3549
@prabhat_3549 4 года назад
Sir please tell me About the statement you wrote Self.dogs.append(self) How did that worked for both objects
@kaustubhnarkhede5166
@kaustubhnarkhede5166 4 года назад
Because, dogs (list) was created as a class variable.. whenever you do self.dogs.append(self.name) it updates the class variable dogs list! Hope that helps! ❤❤
@mayanksinha5816
@mayanksinha5816 4 года назад
great stuff
@nagdevineni6823
@nagdevineni6823 3 года назад
display object variable stored in Global variable. print(Dogs.dogs[0].name) print(Dogs.dogs[1].name)
@kimoldugumbenihicalakadare7897
@kimoldugumbenihicalakadare7897 4 года назад
hi Tim, ı find your videos really helpful thank you for that but here is the thing ı struggle is that you speak a bit faster ı'm not a native speaker so it makes a bit harder for me to listen and understand when ı slow it down it sounds weird too so could you please try to speak a little bit slow
@isaiah7310
@isaiah7310 4 года назад
On my desktop version of youtube I can click settings on the video and choose a playback speed. He sounds fairly normal at 0.75 speed and you can put in your own custom playback speed if you would like. Setting it at 0.85 would probably work for you. Other than that, I am typing in everything out while listening and regularly skip back 30 seconds or so so that I can re-watch portions to try to understand it better.
@kimoldugumbenihicalakadare7897
@kimoldugumbenihicalakadare7897 4 года назад
@@isaiah7310 thank you!
@MsDareDevil1
@MsDareDevil1 4 года назад
tim = Dog("tim") jim = Dog('jim') print(Dog.num_dogs()) Dog.bark(Dog.num_dogs()) but this does work, the dog barked 2 times :D, you said that it will not work :D
@definitelynotjames1970
@definitelynotjames1970 4 года назад
yea same idk why does it work
@wongkingshun
@wongkingshun Год назад
return len (cls.dog) why do we need to know the length of the dog? i dont get it here
@marcusbrennan341
@marcusbrennan341 3 года назад
Why sometimes do you declare a class as for example class Dog(): with brackets and then also class Dog: and no brakets?
@mryelaer4378
@mryelaer4378 3 года назад
You don't need to do the brackets behind Dog. It is fine to write class Dog: Only when you want to inherit from a other class you need the brackets. Like i.e. class Dog(animals):
@MagnusAnand
@MagnusAnand 4 года назад
“Actívate Windows” 😂
@anselmtuachi3298
@anselmtuachi3298 2 года назад
I just read the whole comments and discovered I’m the only one that didn’t understand the concept🥺🥺🥺🥺
@sembutininverse
@sembutininverse 3 года назад
❤️❤️
@The_Short_Pass
@The_Short_Pass 3 года назад
dude you have 666k subscribers
@TechMarketers
@TechMarketers 3 года назад
This example with dogs does not make sens.
@kiranp5828
@kiranp5828 4 года назад
Nice video series.But i want to know why he was feeling sleepy at the time of recording.Thats just disgusting to hear while he was speaking with that sleepy tone.I recommend to change your tone man
@m.t-thoughts8919
@m.t-thoughts8919 5 лет назад
So with @classmethod you can create bassically "subclasses". 🤔
@isaiah7310
@isaiah7310 4 года назад
Creating sub-classes would be in the 3rd video from this series, the one on inheritance. The sub-classes would be the child classes you create that inherit the attributes from the parent class. The @classmethod is a function within a class that is passed it's own class. It is bound to the class itself as opposed to an object. It, like the static method, does not require a creation of an instance of that class. The @staticmethod is a function that doesn't know anything about the class, it just deals with the parameters. Hope this helps.
@m.t-thoughts8919
@m.t-thoughts8919 4 года назад
@@isaiah7310 Thx buddy. It does. It is the most confusing topic in Python for me.
@biskit7
@biskit7 5 лет назад
your title spelling "Mehtods"
@tyffhabwe8066
@tyffhabwe8066 5 лет назад
Great vid but you misspelled 'methods'
@sanjarsaidov9765
@sanjarsaidov9765 2 года назад
Activate your windows😀😀🤣🤣😂
Далее