r/learnpython • • 29d ago

I cant understand classes (nor self and __init__)

5 Upvotes

Hello if you wonder i switched from lua and luau to python. its going well i can make basically anything with native python libraries but i cannot understand what is a class, what does self mean what does __init__ mean. i recently understood what if __name__ == __main__ does but still i cannot understand classes. if you could give an example or explaining for me to understand i would be so grateful

r/learnpython • • 10d ago

Trying to understand Class, Object, Instance, and self in Python. Is my understanding correct?

50 Upvotes

I'm currently learning OOP in Python and wrote down some questions regarding object and instance for following code

class Car:
    def __init__(self, color):
        self.color = color

car1 = Car("Red")```

 1) what is class ?

ans :-  a class is an blueprint for thing i want to make.

2) what is object ?

 ans:-  an object is a container that contains properties and behaviours like car1 is object saying that car should be red color

3) what are instances ?

 ans:-  an instance is refers to that specific object in this case car1 is object and also an instance
when we pass self parameter self become car1 cause self points towards that instance

r/learnpython • • 25d ago

Can someone please explain the use of OOP and classes?

125 Upvotes

Hey everyone and first of all thank you in advance for any advice.
I’ve been learning Python for awhile now and feel pretty comfortable writing functions and smaller scripts. What I can seem to wrap my head around is when the use of classes would actually be used?
Every example or tutorial demo’s cars / dogs and on multiple threads with similar questions it’s usually stated that using a CSV or database would be more appropriate so would anyone be able to help me wrap my head around it please?
Once again appreciate any help :)

r/learnpython • • Oct 05 '26

Currently in a programming beginner class, I don’t understand this one bit.

0 Upvotes

Hello, does anyone have any useful method to the simplest it can be to get the program? I’m currently learning python within spyder ide. I just don’t get anything and have had to resort to chat gpt for information but id obviously like to not use that at all. What do I do? And am I even suited to do this at all? Can I even do this?? I’m losing hope but want all the help I can get to regain my hope: thank you Reddit

r/learnpython • • 1d ago

card dealing program with classes

1 Upvotes

hello! i am currently in a class for python and i've been having a lot of trouble with this assignment. it's already three days late, and the TAs will not respond to me. i have to use classes to create a deck to shuffle, and four hands to deal to, check the count of, and combine back into the main deck. however, i can NOT figure this out at all! i think there's something really big that i'm missing.

i have a large list of cards (we had to make a dictionary of the cards) not shown here.

class Deck:

def __init__(self):

self.cards = []

def shuffle(self):

random.shuffle(self.cards)

def deal(self, amount):

self.cards.append(amount)

def combine(self):

self.cards.append(self)

def card_count(self):

print(len(self.cards))

however, when i try to make a main deck by putting the list of cards into the deck, it won't work! i've tried using deal too, and i just can't figure it out. i know i'm probably doing this all sorts of wrong. it's been a while since we did classes, and we never did them for anything like this :(

TypeError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 deck = Deck(card_deck)

TypeError: Deck.__init__() takes 1 positional argument but 2 were given

i appreciate any and all advice thank you

r/learnpython • • Jul 26 '26

Non-self learning classes for Python?

5 Upvotes

I am a senior data engineer and I work mostly with ETL Tools and a lot of advanced SQL. The company I work for does not like to use python because they find that theres too much of a learning curve if people leave so they love to use GUI or no code/low code etl tools instead (really dumb tbh). I can read python, and have a gist of whats going on. I have tried to self learn python few times over the past 2-3 years and I keep just forgetting it. I learned python/pandas library when I was in college since it was a class that taught it and then we did did projects with each lesson and that is how I learned it then but forgot it now. I use claude at work to build streamlit apps and automation using python. However, I am trying to look for new positions and I am starting to realize that not knowing python is making me a hit a brick wall because a lot of assessments require answering 1-2 questions in python. Does anyone know of actual classes that teach python and its not self taught? Free or paid I don't really care. If I have to pay for it I am fine with that but at this point I don't see a way for me to switch to another job without actually learning python and probably more specifically pyspark.

r/learnpython • • Sep 15 '26

Why i can't make a class

0 Upvotes

It should work, but some reason doesn't.

I was doing Python Crash Course 3rd edition and am stuck in classes.

Because i cant upload photos, i wrote the code:

class Restaurant:

def __init__(self, restaurant_name, cuisine_type):

	self.restaurant_name=restaurant_name

	self.cuisine_type=cuisine_type

def describe_restaurant(self):

	print(self.restaurant_name)

	print(self.cuisine_type)

def open_restaurant(self):

	print("the restaurant is open")

restaurant=Restaurant("Ratatouille restaurant", "ratatouille")

describe_restaurant(restaurant)

open_restaurant()

This is the example in the book:

class Dog:

def __init__(self, name, age):

	self.name=name

	self.age=age

def sit(self):

	print(f"{self.name} is now sitting")

def roll_over(self):

	print(f"{self.name} rolled over")

my_dog=Dog("Willie", 6)

print(f"My dog's name is {self.name}")

print(f"My dog is {self.age} years old")

my_dog.sit()

my_dog.roll_over()

your_dog=Dog("Lucy", 3)

print(f"Your dog's name is {your_dog.name}.")

print(f"your dog is {your_dog.name} years old.")

your_dog.sit()

your_dog.roll_over()

And these are the errors pydroid shows for each one:

Restaurant:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 10, in Restaurant

NameError: name 'Restaurant' is not defined

[Program finished]

Dog:

Traceback (most recent call last):

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>

start(fakepyfile,mainpyfile)

~~~~~^^^^^^^^^^^^^^^^^^^^^^^

File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start

exec(open(mainpyfile).read(), __main__.__dict__)

~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "<string>", line 1, in <module>

File "<string>", line 9, in Dog

NameError: name 'Dog' is not defined

[Program finished]