Python OOP Tutorial 1: Classes and Instances
Why Stack Overflow recommends it
Stack Overflow users link it when a beginner is confused about classes versus instances, __init__, and self, and needs a gentle starting point for OOP in Python.
Recommended by 10 people across 14 different months, 2017 to 2021.
What it is
The first video in Corey Schafer's Python OOP series, a screencast tutorial on defining classes, creating instances, and writing __init__ and methods, using an Employee example. It runs about 15 minutes and links the rest of the series and the code on GitHub.
What people said
Strong rec Timaayy · 2020-07 · Stack Overflow · on “Python understanding classes and functions”
In the first section of your code you are trying to define class attributes. These are attributes that do not change between instances of your class. On the other hand if you define variables in the def init (self) method these are parameters you must pass when creating the class and will be unique to each instance of the class you create. These are called instance attributes. class Person(): # these are class attributes. name = 'Tom' age = 31 has_job = False class Person2(): def __init__(self, name, age, has_job) # these are instance attributes self.name = name self.age = age self.has_job = has_job In your first code snippet you did not indent the classes attributes appropriately when you c
Recommends Coolq B · 2017-06 · Stack Overflow · on “How do I increase the size of a rectangle if a key is pressed?”
Here's an example of a health bar: import pygame pygame.init() w, h = 400, 400 screen = pygame.display.set_mode((w, h)) health = 0 while True: screen.fill((0, 0, 0)) if pygame.event.poll().type == pygame.QUIT: pygame.quit(); break keys = pygame.key.get_pressed() if keys[pygame.K_x]: health += 0.1 pygame.draw.rect(screen, (100, 240, 100), pygame.Rect(0, 0, health, 35 )) pygame.display.flip() EDIT(added extra features): import pygame pygame.init() w, h = 400, 400 screen = pygame.display.set_mode((w, h)) # I decided to go for a maxhealth type thing here, health = 100 while True: screen.fill((0, 0, 0)) # VVV This line polls pygames events and checks if it a QUIT event, if pygame.event.poll().typ
Recommends Chris · 2017-07 · Stack Overflow · on “Python - accessing parent attribute from child class method is weird”
I agree with @pythad. The video uses poor terminology, teaches very unidiomatic Python, and makes incorrect assumptions about how classes really work. I recommended reading the Python documentation for a brief overview of classes [LINK] , and watching a better tutorial such as this one [this video] . I have not seen all of the series, but the author general seems to teach class and OOP concepts correctly.
Recommends xlDias · 2017-11 · Stack Overflow · on “Python arguments contains an object of own class”
Well in python exists the "self" object as you mentioned.. the self object is kinda like "this" in java but with some differences in your dictionaryParams function the paramone is acting as the self object since you did not declare it as a @staticmethod I really recommend this video [this video] to understand it better and this one [LINK]
Recommends Tobias Jørgensen · 2018-09 · Stack Overflow · on “Calling a method defined in another class”
There are lots of resources at hand with a google search. Maybe start here [this video] and then use that knowledge to ask google the right questions :)
Recommends JohnO · 2019-05 · Stack Overflow · on “How to have multiple versions of the same object, each with different properties”
I think you may be misunderstanding how classes work, or more specifically the difference between classes and instances. It makes sense to have a class for Weapon , but you don't want IronSword to be an instance of that class, you'll want it to be a subclass. In your inventory you'd then have two instances of the class IronSword with specific enchantments. Alternatively, you might not do an explicit subclass for iron swords but simply instantiate multiple weapons with parameters that identify them as iron swords. But either way, one object should correspond to one object, i.e. each inventory option should be a separate object. Here is a very good series of YouTube videos that give an introdu
Recommends user78910 · 2020-06 · Stack Overflow · on “Python Constructor Overloading”
[LINK] This is the standard way to go about constructors in general. Here is also a nice explanation [this video] I hope this helps! :)
Recommends Vijay · 2020-09 · Stack Overflow · on “how to make object keep moving up”
[this video] this youtuber explains OOP well if you want to check it out. It's an important and a fundamental concept you should try to learn.
Recommends Matiiss · 2020-10 · Stack Overflow · on “Cannot get the score to update when using Tkinter”
Also as I have noticed you can make just one class -> Window for example and then create 2 instances of that class to make 2 pages. You can check this tutorial [this video] for better understanding of classes
Recommends Matiiss · 2021-04 · Stack Overflow · on “Having trouble passing the user's text entry value to the search aspect of the selenium script”
@CoreyGrandmaison I don't know if You get a notification if an edit has been made but I added the code which uses classes: actually here [this video] is a series of tutorials about classes if You are interested (at least I recommend). (and did You receive the notification about the edit? just curios) and obviously if You have any issues just ask