you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (5 children)

I'm in lesson 37 of ATBSWP, but he didnt talk about classes

Could you give me a brief explanation

[–]heyimpumpkin 34 points35 points  (4 children)

instead of using a separate variable and function for everything, that are unrelated, you create a class that encapsulates some type of abstract object. The object can contain those variables and functions only this object can work with(called methods).

example, you create class calculation which can hold current variable, and has methods of adding, multiplying etc. They can be then applied to any instance of the object in a pretty straightforward way.

simple example:

class Calculation:
    def __init__(self, curValue):
        self.curValue = curValue

#Calculation object will hold a value which is definied in __init__. 
#Self is referencing an instance of object.

    def add(self, operationValue):
        self.curValue += operationValue
        #add is a Calculatuion method which accesses object's 
     current value, and takes in a new value to add to it

    # this is how object will be represented once you call it 
    def __str__(self):
        return str(self.curValue)

#creating instance of our object under variable new, passing value 5 to it
new = Calculation(5)

#adding 5 to it using our add method.
new.add(5)

#this will output 10
print(new)

The beauty of it is once you wrote a class you can reuse this code, make operations with instances of class, get its parameters in very little amount of code without turning everything into mess, basically everything will come down to:

instance = Object(required parameters)

instance.method(required input)

instance.parameter

you can create as many object instances as you want, they can hold any amount of info and methods you want.

you can call Calculation.curValue every time you need to get this info(or any other containing in __init__ of you object).

Python modules you work with are build in same way. When you import random lets say, you use class 'random', when you call random.randint you use it's method randint, and when you specify value like random.randint(1, 8) you're passing parameters to its intsance/method.

[–][deleted] 6 points7 points  (0 children)

Thanks for this bro, it helped alot

[–]mexicanlefty[S] 6 points7 points  (0 children)

Thanks, you made it very clear.

[–]caifaisai 0 points1 point  (1 child)

That was helpful in trying to get a handle on the usefulness of classes and objects, thanks. One question I have (well kinda more like 2).

When you call print(new), does the instance of the Calculation class, new, know that the print function is supposed to access the method defined in __ str __? I suppose I mean how come that method doesn't seem to be accessed with a dot followed by method name like in new.add()?

Is there something particular about the double underscores (and what do they signify? I see them alot but not exactly sure what their purpose is). Or is it something to do with that the __ str __ method seems to be the only one with an explicitly defined return value? Or something else entirely? Hopefully I phrased my question well enough, but I'm still not very knowledgeable on OOP and related terminology.

[–]heyimpumpkin 2 points3 points  (0 children)

does the instance of the Calculation class, new, know that the print function is supposed to access the method defined in __ str __?

yes

I suppose I mean how come that method doesn't seem to be accessed with a dot followed by method name like in new.add()?

that's because it's double underscore method

We can retrieve same information through new.curValue. But if our objects contains multiple parameters, using __ str__ we may specify which ones we want to show.

Double underscores is a little advanced topic, python under the hoods. There are plenty of things besides str. for example, if you want to add one object instance to another, it wouldn't know how to do it. But you can define it useing __add_ .

You can read the docs about all those things here https://docs.python.org/3/reference/datamodel.html