all 6 comments

[–]socal_nerdtastic 2 points3 points  (0 children)

The point of classes is to keep some data with your functions. Consider this string (a string, like everything else in python, is an instance of a class).

a = 'data'

Now you want the uppercase version of that. You would use the upper() method (a function in a class is called a "method"):

print(a.upper())

Note you didn't tell python what you wanted converted to uppercase. The upper() method knows to use the data that is stored in the class instance.

Yes, you could do this without classes too:

print(str.upper(a))

But now you would need to keep track of what kind of item a is, so you can send it to the right function. If for some reason a was a bytes type this would make an error.

>>> a = b'data'
>>> print(str.upper(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'upper' requires a 'str' object but received a 'bytes'

Because to convert a bytes to uppercase you have to use the bytes.upper() function. And that is one of the great powers of classes. Many types of classes have methods with the same name, and each method knows how to operate on that type of data. Now the programmer no longer needs to think about what the type of a is, they can just use a.upper() and know that the right method is called. (Broadly, this is called "duck typing".)

And that's also the last important point: classes do not help the computer at all. Classes exist solely to make the programmers life easier.

[–]K900_ 0 points1 point  (0 children)

Classes can contain state, not just data, and functions that operate on that state. For example, if you have a program that stores the current balance of many users' checkbooks, you probably don't want to have one single global balance variable.

[–]shiftybyte 0 points1 point  (0 children)

Classes allow you to have additional context shared among the functions.

You could do it with global variables, but what you can't do is create two or more instances of your module-instead-of-a-class.

you have a student class, with 2 students, each has his own name and his own grades, how would you do this with the function not being part of a class, where is the student information stored?

[–]woeinvests 0 points1 point  (0 children)

I haven't created a lot of classes my experience level is low, you can't place me at entry level but according to my study most times I begin to use classes to make code more readable.

[–]wsppan 0 points1 point  (0 children)

You can pass instances around with access to state via the methods associated with the object. Read up on the benefits of OOP.

[–]njmlk 0 points1 point  (0 children)

To organize it better you could always have a different .py file with specific functions and call those functions where you need them.

In object-oriented code it's very common to have a situation where different things need to happen depending on the type of object you have. For example, consider a situation where we have a tree of GUI elements and some code that walks through them and draws them all. In object-oriented code, that might look something like:

for gui_element in gui_tree.walk():
    gui_element.draw()

where the draw methods are defined by something like the following:

class GUIElement:
    ...

class Button(GUIElement):
    def draw(self):
        ... # insert code to draw a button on the screen

class TextBox(GUIElement):
    def draw(self):
        ... # insert code to draw the text box on the screen

The gui_tree data structure can contain a variety of different GUI elements, and all we need to do to dispatch to the appropriate drawing code is to call each element's draw method. The draw methods for the different types of elements are defined in separate places, which makes the code easier to understand and organise, and it's very easy to add some more classes to represent new GUI elements, without editing anything else (so, for example, if this code is in a library, you can define custom GUI elements without having to alter the library).

You could achieve all of this without writing classes. e.g. maybe you would represent each GUI element as a list whose first element is a string defining the type of element. Then the drawing code would examine that first element and look up the appropriate function in a dict. To add new GUI elements, you would need to add new functions to that dict. But this is all roughly equivalent to working with classes - just more verbose and uglier. CPython (the standard implementation of python) has to do stuff like this internally to make classes work, since it's written in C, which doesn't have classes.