Chapter on classes is completely unclear to me 😟

Hi everyone,

Yesterday I started on chapter 10 of the Python 3 course, which is on classes, and I’m completely baffled. I’ve done all the preceding chapters and challenges and sure, some concepts or exercises took me longer than others, but the concepts were always explained in a way that made sense to me.

In this chapter, not so much. I can get by doing the exercises and I know what is expected of me here, and I kinda understand how it works so far. But I just cannot understand the underlying concepts at all.

I’m now on Instance Variables, part 9 of 14 exercises. Here the explanation states: “We’ve learned so far that a class is a schematic for a data type and an object is an instance of a class, but why is there such a strong need to differentiate the two if each object can only have the methods and class variables the class has? This is because each instance of a class can hold different kinds of data.”

… what? Just what? I do not begin to comprehend what they are trying to teach me here :frowning:

Is there anyone here who can explain to me:

  1. why use classes at all? What do classes accomplish that functions can’t?
  2. is there a really down-to-earth, easy comparison to be made between a class and a real-life thing, just so I can understand it better?
  3. or is there a book, website, video where this is explained for the kind of total n00b I am?

thanks!

I suspect you’ve understood all there is if you’re asking this.

classes let you bundle up data and their behaviour.
Think of a string. It contains the “what is this text” kind of data, and also has behaviour like converting to up/down case.

In python, classes let you give behaviour to values that is recognized by the language, things like being iterable, convertible to string, callable (like a function), indexable, operators like +- etc

You might have some really deeply nested abstraction, maybe you have lists in dicts in tuples in… it starts getting unmanageable at some point and classes can help by giving names to those different things, and also help ensure that the structure makes sense. You could do this with functions too, but you lose some language support.

5 Likes

welcome to one of the topics where programmers can debate about for a long time. If you google: functional programming vs oop, you get an almost endless list of articles. Given the amount of articles, i recommend you to read a couple and ask if you have any questions about them, then i will try to answer them

lets compare it to building houses, a class is a blueprint, so once we have the class we can rapidly build houses (by creating objects, instances of the class)

just like with building a real house, we can have slight difference between the houses (one house might have an oak front door while another house might have a maple front door)

using different resources is a good idea, get OOP explained different ways until you find an explanation that clicks. googling: python OOP, surely should yield some good results

quite frankly, i truly started to understand classes once i got to work with a “larger” codebase.

5 Likes

Thanks for your detailed reply! I did some more reading and I think I’m starting to understand.

I think the whole point of this is that classes are useful when you are writing code about stuff that is messy. Describing the books in a library is easy, because there is a finite list of useful properties (title, author, nr of pages, publisher, etc) and every book has all of the properties listed. But when describing people things are a lot more messy. Most people have two legs, hair, and can talk, but not all. Classes make it possible to define People() which have some overarching properties as class variable, such as breathing (I would hope) and inhabiting planet Earth. Individual instances of Human() can have instance variables describing hair color and number of legs (hopefully 2).

I do think this will result in an awful lot of stuff to keep track of in the long run, but it has its advantages I suppose :grinning:

1 Like

You could do that with a dict and call it a day. Nest them all you want.

But the dicts themselves would have no concept encoded into them about what they represent.

1 Like