-1

I got a question that quite disturbs me a lot and I think it might help a lot if I had an answer to it. So I got this:

class Klasse1:
    variable1 = "haha"
class Klasse2:
    variable2 = "hoho"

print Klasse1.variable1
print Klasse2.variable2

and this:

class Klasse1:
    variable1 = "haha"
class Klasse2:
    variable2 = "hoho"

Object1 = Klasse1()
Object2 = Klasse2()
print Object1.variable1
print Object2.variable2

Now the question is: Why would I use the second(too much code) instead of the first(obviously easier.) In many sites and tutorials I've seen people create objects for something they could easily get without creating them. So I'm probably missing something and I would like to know.

P.S. Same case with Java

closed as unclear what you're asking by gnat, user22815, durron597, user40980, JB King Nov 5 '15 at 18:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

In your small example it makes no difference between the first and second example. Either is fine.

The difference becomes important with real world examples, such as when you want to have multiple instances of a class. For example, consider a game. If you are a single-player game, your first example is fine. Create the class, and reference the values directly in the class.

However, what do you do when you have a multiplayer game and variable1 represents the player's score? You can create a Player1 class and a Player2 class, and your first example continues to work. Now what do you do if in addition to score, you have 100 other attributes? Do you have two identical classes that are defined exactly the same? What if you want to support 100 players? Or 1000? Do you create 1000 classes that are identical, only differing by the name of the class?

This is why classes are so powerful: you define the class exactly once, and then you can create as many instances of that class that you want. Whether you have one player or 1000, you only have to define the player class once, yet each class has it's own data 1

So, for simple examples, why go to the trouble of creating instances? The reason is that it builds good habits. If you get into the habit of creating classes and instantiating them, when the time comes to make a more complex application you won't have to change the way you work.

Bottom line: for any non-trivial definition of an object, your code will be much easier to maintain if you have a single class with multiple instances.


1 if we literally use your example code, each player will share the same score. However, if you create classes with instance attributes rather than class attributes, each player will get its own score.

2

Think of classes as cookie cutters and objects as cookies.

Each cookie has the same shape; that of the cookie cutter. But each cookie might have a different color: color = "green"

You would do it the second way because you would never imagine baking a single cookie during your entire lifetime, nor would every cookie you bake be green.

0

Why would I use the second(too much code) instead of the first(obviously easier.)

Just so we're clear, I assume you know difference between class and instance variable. If you do, you know that it makes no sense to actually create an instance of a class just to access (read/write) a class variable. That would be absurd.

So I'm going to reinterpret the question like this:

If I have an instance Object1 of class Klasse1 and I wish to access a class variable variable1, should I use Klasse1.variable1 or Object1.variable1?

Technically both are correct.

IMHO you must use Klasse1.variable1 as a matter of convention (even when you had an instance of Klasse1 at hand). It's cleaner code; it tells reader that you're accessing a class variable and not instance variable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.