Python Object Oriented Programming (OOP) - For Beginners

Tech With Tim · 4.1M views · 2020 · 53:06

Open on YouTube

Python, Ruby & scripting languagesEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering beginner questions about classes, self and constructors, and when asking for a refresher on OOP concepts.

Recommended by 4 people across 5 different months, 2021 to 2024.

What it is

A 53-minute beginner tutorial by Tech With Tim on object oriented programming in Python. It walks through classes, objects, constructors, methods and inheritance with live coding examples.

What people said

Recommends carlosdafield · 2021-11 · Stack Overflow · on “Don't understand cause of this TypeError”

You have two issues here. 1) A class is meant to create as an object, so do obj.calc_bmi(weight,height) 2) self.wieght and self.height need to be set by the values passed in Here's a working solution class bmi: def __init__(self): self.weight = 0 self.height = 0 def calc_bmi(self,weight,height): self.weight = weight self.height = height if self.height<=0: print("!!!DIVISON BY ZERO ERROR!!!") return None self.bm = self.weight /self.height * self.height return self.bm def main(): obj = bmi() weight = float(input("Enter weight")) height = float(input("Enter height")) result = obj.calc_bmi(weight,height) print(f"Your bmi is {result}") main() Watch this video and you will understand everything (

Recommends carlosdafield · 2021-12 · Stack Overflow · on “How do I rewrite this code to fetch the details from a table from postgres DB in OOP format?”

Heres an example of the same code but with a class. class ConnectDB: # Special method that runs when initialized def __init__(self): self.db = self.connect() def connect(self): return psycopg2.connect(host='localhost', database='Country Details', user='postgres', password='root') def _tuplesToStrings(self,rows): return [rows[row][0] for row in range(len(rows))] def getCountries(self): cur = self.db.cursor() cur.execute("SELECT country_name FROM country_details") rows = cur.fetchall() country_list = self._tuplesToStrings(rows) cur.close() return country_list # Special method that runs when the object falls out of scope. def __del__(self): self.db.close() And heres how you would use it. db = C

Recommends ori raisfeld · 2023-02 · Stack Overflow · on “My Simple combat system isn't working properly”

you only lower the health if health is above 0, so it's going to repeat forever for no reason def health_damage(): sum = int(d_health[0]) - damage if sum > 0: d_health.remove(d_health[0]) d_health.append(str(sum)) begin_game() if int(d_health[0]) <= 0: clear() print( "You defeated the dragon!" ) just remove the sum part and directly remove the if statment. it doesnt help with anything. you should instead do this: d_health = 100 damage = random.int(1, 7) def health_damage(): d_health -= damage # the -= is a better looking version of d_health = d_health - damage if int(d_health) <= 0: clear() print( "You defeated the dragon!" ) while True: if input(""): quit() else: clear() begin_game() (btw a

Recommends agustinvaca · 2023-04 · Stack Overflow · on “How do I get the return value from a function?”

As detailed in this answer [LINK] , you can either declare a global variable before on_click() , and set that variable inside on_click() using the global keyword before the name of the variable. It would be preferable however to create a class for your button, and create a member variable that you set inside an on_click() method and can later access with a getter method. If you need a refresher on OOP concepts, I would recommend this article [LINK] , or this video [this video] .

Recommends Advik · 2024-06 · Stack Overflow · on “How do I use tkraise on another frame class in Tkinter”

The Problem The error you're encountering is because the sign_in function is defined inside the widgets method, making it a nested function. In this context, it does not automatically get self as its first parameter. To fix this, you need to move the sign_in function to be a method of the login_page class instead of being inside widgets. Additionally, to raise the main frame after a successful login, you need to pass a reference to the main frame instance to the login_page instance. Here’s a corrected version of your code: from customtkinter import * global d_text d_text = '' class software(CTk): def __init__(self): super().__init__() self.geometry('1000x800') self.Main = main(self) self.Log

Cites Swerve · 2021-05 · Stack Overflow · on “get_grade() missing 1 required positional argument: 'self'”

I was trying to Imitate a video ( [this video] ) I'm facing the problem whenever I run the code I got an error for the 'self' argument * the first line should be with no indent but for StackOverflow I indeed the whole code with 4 taps. class Student: def __init__(self, name, age, grade): self.name = name self.age = age self.grade = grade # 0 - 100 def get_grade(self): return self.grade class Course: def __init__(self, name, max_students): self.name = name self.max_students = max_students self.students = [] def add_student(self, student): if len(self.students) < self.max_students: self.students.append(student) return True return False def get_average_grade(self): value = 0 for student in self.stud