-1

I've looked at online Python tutorials, and one specific aspect I'm having trouble understanding are classes.

I been on Codecademy, and also many Pygame tutorials. There are sections that include classes, but I don't understand the functionality of them, or how to use them.

I understand that the code is something like this:

class someClass():
    def __init__(self, thing1, thing2, thing3):
        self.thing1 = thing1
        self.thing2 = thing2
        self.thing3 = thing3

I just don't understand how you would use them and what you would use them for. If anybody could help me, I would really appreciate it!

4 Answers4

1

Welcome. What are you trying to accomplish?

You could probably use only functions/coroutines, depending on what you try to do. In fact, for plain calculations, I find it sometimes easier that way.

Python support both paradigms (functional/object oriented).

Object oriented programming is a good way to model real life objects. For instance, let s say you are writing a game that has many characters, you could create a Character class which would be your blueprint for all of them.

Even better, you could look at a concept called composition, and mix and match objects to create a certain concrete instance. That way, a Building class could hold pointers to 20 concrete instances of an Appartment class, an instance of your SwimmingPool class and so on.

Dan Mahowny
  • 163
  • 1
  • 10
  • How I'm understanding it, you're saying that a class is a base for a bunch of different types of things that share similarities? Meaning that I could be able to create a bunch of different objects that are the same thing? – Xander Kleiber Jun 07 '19 at 00:30
  • Yes it could be. Read up on 'composition vs inheritance', and think how this could be used in your software. – Dan Mahowny Jun 07 '19 at 02:38
0

Short: a lot of things, almost everything you want

Long: classes is the part if oop(object-oriented programming), where everything is an object and you can define behaviour of different objects as well as their interactions. It js impossible to explain all its possibilities in this answer. Try to read more about oop to get deeper.

0

Classes are like object, things we will use to hold data and do functions.

For example: a class person will have the variables name, address, favorite fruit, etc...

class person():
    def __init__(self, thing1, thing2, thing3):
        self.name = thing1
        self.address = thing2
        self.favorite_fruit= thing3

Init is performed when creating so thing1, thing2 and thing3 are the parameters you are passing when creating. Otherwise the will be null.

In another part of the program you might need to create an instance of object person.

 John = person("John", "Elm Street", "apple")
pedro_bb7
  • 1,601
  • 3
  • 12
  • 28
0

Ok so i will try to explain you Classes from my personal perspective which i hope makes sence. Note No Syntax followed to increase readability.

A Class is a collection of objects. Class is a structure where we can define varibles and methods just like a Blueprint.

For example
class (Car) -> Object 1 = Audi, Object 2 = BMW

So in this example, The car class is a structure where we can define the varibles and methos (since all cars follow the same working) which is utelised by the objects.

Another example

Class SavingsAccount -> Object (Account_Num, Account_Holder)
A Person (Object) who has the Savings Account (Class)
Orange is an object of Fruit Class
Chrome is an object of Browser Class

How any why we use them, Well Just like Lego, 1 part of the code is used to build a house. We dont want to write the same code again and again when we can write 1 code (Class Car) which can be used by any other companies like Audi/BMW (Objects)

Hope this explains it :)

mccrudd3n
  • 1
  • 2