r/learnpython • • 28d 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 • • Oct 05 '26

I’m taking a “learning programming in python” class in high school and im already lost.

43 Upvotes

The first month was very easy. Inputs, basic programs and whatnot. Now its while and for statments, as well as loops and nested loops. The programs are mostly solving hard mathematical equations, and I’m not great at math and the rest of the class is very good, so I’m always behind.I am soooo lost now and rely heavily on the internet to finish my programs. Is there anything i can do outside of class to gain the same programming knowledge. The textbook were studying is starting out with python 6th edition by tony gaddis.

r/learnpython • • 25d ago

Can someone please explain the use of OOP and classes?

124 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 • • 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 • • 9d 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 • • Feb 23 '21

Classes. Please explain like I’m 5.

229 Upvotes

What exactly do they do? Why are they important? When do you know to use one? I’ve been learning for a few months, and it seems like, I just can’t wrap my head around this. I feel like it’s not as complicated as I’m making it, in my own mind. Thanks.

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]

r/learnpython • • Sep 03 '26

I’m taking a programming (intro to python ) class and the teacher isn’t the best in the world so I’m thinking of trying to learn on my own .Any advice I’m an extremely visual person so videos help me more than books

0 Upvotes

Thanks!!!

r/learnpython • • Apr 18 '26

Beginner: Want to learn Classes.

20 Upvotes

I find classes to be very confusing. The way variables are used. Self comes to me in a very confusing manner. i just can't seem to wrap my head around the basics of Classes.

Also i just tried checking OOP and i think it just overloaded my brain. Anything to help my case?

r/learnpython • • Sep 20 '26

Tips on reinforcing intro to ds class (python)?

10 Upvotes

Hi everyone,

This semester I just added data science as a minor and I am now taking my schools intro to data science class.

To give some background of the course and myself: I have never coded before (besides scratch), it was just revamped so we can use ai on everything (though I prefer not to except help when I’m stuck) and we use colab to code.

When I’m in class everything makes sense to me; I know what everything is, what each method function does; I can read code well. However, when it comes to building my own things, very simple ones, I get stuck. It seems like I have a general direction of where to go but I’m always looking back at examples, and at that point I feel like I’m not even doing it myself. An example of a prompt could be:

create a personality test, similar to buzzfeed, that asks the user 3 questions.

Does anyone have any advice to help reinforce the writing aspect?

r/learnpython • • Jan 24 '26

How long should I spend on basics (loops, conditionals, functions, classes) before moving to advanced Python?

21 Upvotes

I’m learning Python and I’m unsure how long I should stay on the fundamentals before moving on.

Right now I understand:

  • loops (for, while)

  • conditional statements

  • functions

  • basic classes and objects

I can solve small problems, predict outputs, and write simple programs without looking up every line. But I still make mistakes and sometimes need to Google syntax or logic.

Some people say you should fully master the basics before touching advanced topics, while others say you should move on and learn the rest while building projects.

So realistically:

  • How long did you spend on these basics?

  • What was your signal that it was okay to move forward?

  • Is it better to set a time limit (like weeks/months), or a skill-based checkpoint?

Would love to hear how others approached this.

r/learnpython • • Oct 29 '25

Everyone in my class is using AI to code projects now is that just the new normal?

499 Upvotes

so our prof basically said “as long as you can explain it, you can use it.”

and now literally everyone’s using some combo of ChatGPT, Copilot, Cursor, or Cosine for their mini-projects.

i tried it too (mostly cosine + chatgpt) and yeah it’s crazy fast like something that’d take me 5–6 hours manually was done in maybe 1.5.

but also i feel like i didn’t really code, i just wrote prompts and debugged.

half of me is like “this is the future,” and the other half is like “am i even learning anything?”

curious how everyone else feels do you still write code from scratch, or is this just what coding looks like now?

r/learnpython • • Apr 20 '26

Which tutorial/ Website helped you understand OOP and Classes ?

8 Upvotes

I have used W3 schools and I understand the concept but I don't grasp It fully .

r/learnpython • • Mar 16 '23

Been using Python for 3 years, never used a Class.

611 Upvotes

This is not a brag, this is a call for help. I've never been able to get my head around what a Class is. People tell me it's a container for functions, then why can't you just use the functions without a class? I just don't understand the concept of a class and why I would use one. Could someone please help? I've watched numerous videos that just make Classes seem like glorified variables to me.

r/learnpython • • 8h 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 • • Jun 23 '26

In a coding interview, if I'm expected to "write tests" for a simple Python class, what does that mean?

24 Upvotes

I heard I may have to "write tests" for a simple Python class. What's that? How would that look? What would I be expected to do in a live coding interview?

I can make basic variables and basic methods in a Python class, and I can instantiate an object of that class, but tests?

Is that complicated or time-consuming to learn?

(By the way this isn't for a software engineering role. It is for a scripting related role, so the interview is not expected to be as complicated/advanced as it would be for a software engineering role.)

r/learnpython • • Nov 28 '25

Learning classes - ELI5 why this works?

16 Upvotes
class Greetings:
    def __init__(self, mornin=True):
        if mornin:
            self.greeting = "nice day for fishin'!"
        else:
            def evening():
                return "good evening"
            self.__init__ = evening

print(Greetings().greeting)
print(Greetings(mornin=False).__init__())

So this evaluates to:

nice day for fishin'!
good evening

I'm a bit unsure as to why this works. I know it looks like a meme but in addition to its humour value I'm actually and genuinely interested in understanding why this piece of code works "as intended".

I'm having trouble understanding why __init__() "loses" self as an argument and why suddenly it's "allowed to" return stuff in general. Is it just because I overwrote the default __init__() behaviour with another function that's not a method for the class? Somehow?

Thanks in advance! :)

r/learnpython • • Jul 29 '26

Is there a difference between using a Class name v self in the definition of a class

17 Upvotes

Hi

Im doing some tutorials online and in one of them I noticed some syntax that I am not familiar with. It looks like instead of using self to assign values to a class it is using the class name.

In terms of behaviour it seems to make no difference but I cannot understand why a person would not use self.

So for example:

class Demo:
  def __init__(self):
    self.a = 1
    Demo.b = 2 # This seems to work the exact same way as self.a
               # Is there a differences to self.a?  Why would someone use this?

Thanks

r/learnpython • • Apr 18 '26

A bit confused in Classes.

39 Upvotes

Why do i need to call self here?.

class Calculator:
  def add(self, a, b):
    return a + b

  def multiply(self, a, b):
    return a * b

print(Calculator().add(1, 2))

there isn't a variable that is calling calculator and no __init__ so why do i have an error if self is not added?

Also, what is __init__ anyways. why the double __ in the start and end? and why the specific name?

r/learnpython • • Mar 04 '26

Is print() a function or a method in Python? Getting mixed explanations in class

66 Upvotes

I’m currently teaching Python fundamentals and ran into a confusing explanation about print().

My understanding has always been that print() is a built-in function in Python. It’s part of Python’s built-ins and you can call it directly like:

print("Hello")

But my education coordinator explained it differently. He said that print is a method because it’s already there, and that functions are things you create yourself. He also said that methods take arguments and functions take parameters.

That explanation confused me because everything I’ve read says:

  • print() is a built-in function

  • Methods are functions attached to objects or classes (like "hello".upper())

So now I’m wondering:

  1. Is there any context where someone would reasonably call print() a method in Python?

  2. Am I misunderstanding the difference between functions, methods, arguments, and parameters?

I’d appreciate clarification from more experienced developers because I want to make sure I’m explaining this correctly to students.

Thanks!

r/learnpython • • Jul 26 '26

i wanna learn python and im broke for classes so try to teach me ig

0 Upvotes

If anyone wondering i just want to learn for fun

r/learnpython • • Aug 31 '26

how do i dymanically generate large numbers of objects in a class/dictionary?

7 Upvotes

my goal is to create an army manager for the tabletop game mythras, where each soldier has their own hp, attack value, etc, that i can use to add detail to the large scale battles. i want to be able to direct commands to it (eg, 'damage 3 soldiers') and have it randomly determine which soldier is attacked, how much damage it does, and whether it kills them (as well as how it heals up, following the standard healing rules). I don't have a lot at the moment at all, just a dictionary that contains a nested dictionary for each soldier's hp and attack value, randomly determined:

from random import randint

Soldier1 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}
Soldier2 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}
Soldier3 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}

myArmy = {
  "Soldier 1" : Soldier1,
  "Soldier 2" : Soldier2,
  "Soldier 3" : Soldier3
}

print(myArmy)
from random import randint

Soldier1 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}
Soldier2 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}
Soldier3 = {
  "HP" : randint(1,10),
  "ATK" : randint(1,10),
}

myArmy = {
  "Soldier 1" : Soldier1,
  "Soldier 2" : Soldier2,
  "Soldier 3" : Soldier3
}

print(myArmy)

now, that works fine for the three soldiers listed there, but i don't need 3 soldiers, i need 300. how can i input a number and have it create that many soldiers on the fly?

and, further to that, are dictionaries the best choice for this? i know the technical difference between dictionaries and classes, but i don't have any real-world context to either to know which is best for what i need.

r/learnpython • • Jan 13 '26

I cannot understand Classes and Objects clearly and logically

75 Upvotes

I have understood function , loops , bool statements about how they really work
but for classes it feels weird and all those systaxes

r/learnpython • • Oct 13 '25

Title: Struggling to Understand Python Classes – Any Simple Examples?

23 Upvotes

Hello everyone

I am still a beginner to Python and have been going over the basics. Now, I am venturing into classes and OOP concepts which are quite tough to understand. I am a little unsure of..

A few things I’m having a hard time with:

  • What’s the real use of classes?

  • How do init and self actually work?

  • What the practical use of classes is?

Can anyone give a simple example of a class, like a bank account or library system? Any tips or resources to understand classes better would also be great.

Thanks!