Getting a grasp on classes


I'm only starting to understand classes, and when I searched it up, and found out how strings and dictionaries are what they are because they are defined, built in classes (did I understand correctly?), and it clicked for me, but I'm not sure if it's an accurate/correct click. So is it like this, or in the area:

the built in, say, list class gives the list all its attributes, how each value is related to an index, and how new parts of the list can be appended by using the .append() method, and etc.? If this is the case, does anyone know where to find the code of 'list class'? Because I want to truly understand the special attributes and significance of a class, as opposed to a function.

Because, to my understanding, the difference between a class and a function's uses are nonexistent. In other words, there is a built in list class, but couldn't it be just a built in list function? Also, a class can contain lists, dictionaries, and functions, and is just a way of storing data under objects, and producing objects with similar properties. Well, can't a function do the exact same thing? A function can store functions, dictionaries, lists, etc. Functions also produce outputs/objects with similar attributes. And when a class is called on an input, it just looks exactly like calling on a function. I don't see any difference, only the syntax. Otherwise, to me it seems like a class has the same role as function.

In sum, what is the difference between a class and a function? Sunflowers are plants, whereas dogs are animals. Strings contain text, whereas int and float data types store numbers. Now, what does a class have that a function can't do?

Please clarify my confusion. Thank you so much


Then it would not have its own methods to call upon. It would get messy real quick.

Think of a class as being a template for new object instances to be constructed from. A class may contain any number of methods (def) but a function may not contain a class.


But cant a function also contain any number of methods. I'm not trying to be offensive, but saying that a function can't contain a class is useless to me because right now, a class itself seems pointless due to the fact that I see no difference between it and an old fashion function. However, there were things I would like you to elaborate from: what do you mean a class is a template for new object instances. Can't a function also be a template for other instances. Also, can't a function contain any number of methods (def), since a function can contain as many other function inside it as it wants? Thanks for you reply :slightly_smiling:


Just that. It is a template, or model to base new object instantiations upon. The new instances inherit all the methods and attributes of the class, as well as share the same class variables across all the instances.

Suggest you do more reading so you can get this straight in your mind. Lengthy explanations don't really serve any purpose when the documentation is right at hand.


wait a second, right there, i think you said something there. Can a function create instances that inherit all the methods and properties of that function, like a class? FYI, google search is bad, I'm finding tons of things that say class defines a class, and def defines a function, and stuff like that, that doesn't really explain the difference in purposes of functions and classes
So in sum, is it that classes create instances that inherit properties and methods, where functions simply produces an output?


wait, i just found something, is this correct:
"Functionally, you can do the same with both. Pragmatically, classes are
easier to use for all non-trivial data abstraction."



Quoting Alan Gauld from about half way down that page:

To try to answer it I'll start with the big picture answer:

A function is an operation that returns a value. The value may be
None. A function can be used directly by other bits of your

A class is a combination of some data and the functions which
operate on that data all wrapped up as a single item. A class is
a template which is used to create objects. You normally use the
objects in your program not the classes.

To grasp this big picture, we need a solid understanding of how scope works in Python. Let's start with a function.

def func(n):
    return n ** 2

# print n    # will raise an error

print func(5)    # 25

Above, n does not exist outside of function scope, where it is defined in the parameter list. This suggests that we would not use functions to hold data, but merely pass it around. Data into a function through its parameters, data out of the function through its return value. In this way data is exchanged between scopes.

A class on the other hand may be loaded up with data, both shared (class variables) and instantiated (instance variables). Inside a class, functions are methods that are inherited by all instances of the class, but are functionally no different from above. They have one useful variable built in that scopes the functions to their owner object, self.

We no longer have to pass data to the function through the parameter list. Each instance has its own set of which the function can access internally. Instances are what give the methods their execution context. Scope is essentially self containment.


There we go, thats what I wanted from the beggineng. Wow, that really cleared it up, wow wow wow, finally i understand, thank you so much. "I am eternally grateful" #Toystory


It follows that just like functions, methods still can and do take parameters. Consider how we would change the age value, or any value already registered on the instance.

instance['age'] = new_age

would take a method to accomplish, but it would also take a parameter to accept the new data.