0
How to use classes effectively? [SOLVED]
Hi guys. I have problems from a while in using python classes. Someone knows some tutorial or video lesson that explains how to use them?
8 Respostas
+ 4
When modelling real world applications, use classes to model a SINGLE entity.
Example, let's say you wanted to model a football game.
It'll be optimum to use a separate class for modeling the following:
1.Club
2.Ball
3.Players
4.Goal Post
3.Flags
You get the idea, if you are still unclear, please do you YouTube:
CS Dojo
Socratica
+ 2
Sololearn is better than any other resources for you to learn how to class in Python. Because we are a community with many sololearners willing to answer your questions.
The most detailed resource is the Python official website.
https://docs.python.org/3/tutorial/classes.html
For using classes, it can vary greatly between your intention of performing arithmetic operation or for game development.
Let me show you my Python code with class:
https://code.sololearn.com/cKQT42Mk4BlQ/?ref=app
This one focus on magic operator.
For game development, there will be a lot of extensions of classes.
If you have any questions about the magic operators, or how to extend class. Feel free to raise follow up questions.
And if you have coded some attempt, please do share with us so that we can comment.
+ 1
Thanks Gordon!
I've missed the return function
0
When I use the self method and what he means?
0
Thank you for your follow up question specifying the scope to "self".
In simple and short words, the "self" is for ensuring the variable name of different instances of the same objects be pointing to a different
I would like to highlight 9.3.5 of this link:
https://docs.python.org/3/tutorial/classes.html#class-definition-syntax
QUOTE
class Dog:
tricks = [] # mistaken use of a class variable
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks # unexpectedly shared by all dogs ['roll over', 'play dead']
As discussed in A Word About Names and Objects, shared data can have possibly surprising effects with involving mutableobjects such as lists and dictionaries. For example, the tricks list in the following code should not be used as a class variable because just a single list would be shared by all Dog instances:
UNQUOTE
0
Guys, what's going wrong with this?
It doesn't shows the mensal pay
https://code.sololearn.com/czi9k3uVcZCA/?ref=app
0
There are two ways
One way is to return the value in function
Another way is to print in the function
https://code.sololearn.com/cib8W9NoN405/?ref=app
https://code.sololearn.com/cxT5cXYr3ghv/?ref=app
0
Welcome
If your question is solved, can you kindly edit to add a [SOLVED] tag to the question title so that the other sololearners can only this question when they are going to help others.