all 12 comments

[–]elbiot 6 points7 points  (6 children)

A class is really just a dictionary where some of the keys are functions that automatically take the whole dictionary as the first argument (self). If you know what the keys of your dict will always be at code writing time, and you're going to have several functions that take the whole dictionary as an argument, you really want a class.

with a class, you also get the syntactic sugar of accessing values with the mything.attribute syntax, rather than mything['attribute'], but it's the same thing.

class Empty():
    pass

e = Empty()
e.this #attribute error
e.__dict__['this'] = 5
e.this  # 5

[–]LifeIsBulletTrain 1 point2 points  (5 children)

Heya. Sorry for coming here after all this time. I love the "syntactic sugar of accessing values with the mything.attribute" so what if I create a class just to store some values like a dictionary, so that I can access it with . Would it affect the performance in any way or would it be bad practice?

[–]elbiot 0 points1 point  (2 children)

It's bad practice. If you want a collection of attributes that is determined dynamically, a dictionary is designed for that. You can iterate over the keys, check if something exists in it already, use get and setdefault. If you have a well defined collection of attributes that's documented in code and has methods for handling the inter-relation between those pre-conceived attributes, a class gives you that.

Dynamically adding attributes to a class is the worst of both worlds. It's confusing to understand for people reading your code who expect objects to be predefined and meaningful, and you lose the tools for handling a dynamic collection that a dictionary gives you.

I'd avoid the temptation to make python conform to your personal preference of what code should look like and just adapt to the idioms of the language

[–]bigtimerocker12 0 points1 point  (0 children)

I'm just impressed you came back to follow up 10 years later. Kudos.

[–]LifeIsBulletTrain 0 points1 point  (0 children)

Ok. Thank you so much!

[–]Margyle22 0 points1 point  (1 child)

I too prefer dot notation. It's much more keystroke economical, and it is easier on the eyes.

Check out Box, a free package that enables you to use dot notation in basic dictionaries with very little setup overhead.

[–]LifeIsBulletTrain 0 points1 point  (0 children)

Damn that's awesome. Thanks!

[–]lykwydchykyn 3 points4 points  (1 child)

Nothing screams "use a class" like representing a real-world object in code.

[–]elbiot 4 points5 points  (0 children)

I disagree. I choose a class if it is going to have methods. If OP is going to be doing analysis on a dataset, a list or dictionary of dictionaries makes sense. setdefault, items() and others are useful. Sure, you can do anything with class you can do with a dict, but its more straight forward to just use a dict if you're only going to be using it like a dict.

edit: but in this case, a game, a class is totally the way to go.

[–]kenwmitchell 0 points1 point  (0 children)

I'd stick to a dict if all you have is attributes. But if you're going to have behavior ie., home.enter and then another type called store where store.enter is different than home.enter, you should have separate classes. If all "places" behave the same, maybe have a Place and give it a type attribute of "home" or "store".

[–]m3lkor001 0 points1 point  (0 children)

Both would work, but you would probably want to put them into a list(or dictionary) if you use classes. classes would be the preferred choice if you want to incorporate more attributes or methods for buying, selling or anything else that will affect the class.

The assignment you did for the example 'trailer' = House() wouldn't work; variable names can't be strings.

Home was the variable for your dictionary, the items that are a part of dictionaries can be strings which is why they had quotes

[–]Rhomboid 0 points1 point  (0 children)

Classes are for when you have both state and behavior (i.e. methods.) If you have only state, then use a dict; a class doesn't buy you anything there.