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!")
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
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
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.
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.
@@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.
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.
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.
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)
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.
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.
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?
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
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! ❤❤
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
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.
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
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):
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
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.