-1

I have this:

class Klasse1:
    variable1 = "haha"

print Klasse1.variable1

and this:

class Klasse1:
    variable1 = "haha"

Object1 = Klasse1()
print Object1.variable1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    Possible duplicate of [Instance variables vs. class variables in Python](http://stackoverflow.com/questions/2714573/instance-variables-vs-class-variables-in-python) – SiHa Nov 04 '15 at 16:46
  • 1
    In your examples you're only creating *class attributes*, so there's no benefit. Once you get into *instance attributes*, the first version won't work correctly. – jonrsharpe Nov 04 '15 at 16:47
  • @SiHa I disagree that this is a dupe of that. The topics are similar but one is asking "Should I use class variables rather than a singleton" and the other is asking "Why use instances at all" – Adam Smith Nov 04 '15 at 16:56
  • @Adamsmith: Fair point, it woild seem that others agree, as the question is still open... – SiHa Nov 04 '15 at 18:23

2 Answers2

6

You create objects to hold state. If you had something like:

class Wallet(object):
    balance = 0

It wouldn't make sense to share the same balance between multiple instances of the class. You'd want to create a new instance of Wallet for each bit of money you're trying to track.

Because you'd use it by doing something like:

fred.wallet = Wallet
jim.wallet = Wallet  # same object!

So now when Fred spends money

fred.wallet.balance -= 3.50

Jim spends that money too.

>>> print(jim.wallet.balance)
3.50

Instead you'd want to create INSTANCES of the class Wallet and give each one its own state

class Wallet(object):
    def __init__(self, balance=0):
        # __init__ is called when you instantiate the class
        self.balance = balance

fred.wallet = Wallet()
jim.wallet = Wallet()

Now when Fred spends money:

fred.wallet.balance -= 3.50

Jim won't be affected because his Wallet object is a different instance

>>> print(jim.wallet.balance)
0
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
3

This is a tough question to answer succinctly but it comes down to when you want to have more than one instance of Klasse1. If you know you'll only ever need the class and never instantiate it, you'll be fine. If you do instantiate it, (ie calling the class and using the returned object), you can change the variable1 member without modifying the class' variable:

class Klasse1:
    variable1 = "haha"


foo = Klasse1()

print foo.variable1
>>>'haha'

foo.variable1 = 123

print Klasse1.variable1
>>>'haha'

If you made another instance of Klasse1, it's variable1 would still be 'haha' even after altering foo's variable1.

bar = Klasse1()
print bar.variable1
>>>'haha'

What's sort of tricky is that if you change Klasse1.variable1, only bar.variable1 would change, foo.variable1 would remain 123.

Klasse1.variable1 = "this isn't funny"
print foo.variable1
>>>123
print bar.variable1
>>>"this isn't funny"
print Klasse1.variable1
>>>"this isn't funny"
TankorSmash
  • 12,186
  • 6
  • 68
  • 106
  • ***Strongly*** recommend you never try to work on class variables and assume you'll someday want more than one of any object. It's one of those design choices that provides an incredibly small benefit but will be *hell* to refactor later. For anyone still asking why they'd use instances to begin with, this shouldn't even be a consideration. – Adam Smith Nov 04 '15 at 16:57
  • Yeah, class attributes are very tough deal with because you can't easily tell whether you're using that, or an instance's attribute. There's no reason to use them when you're just starting out, if only for this reason alone. – TankorSmash Nov 04 '15 at 17:02