Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, what is the difference between an object and a dictionary?

After an object has been created, I can add and remove slots at will, as I can do with a dictionary. Even methods are just objects stored in slots, so I probably can add methods to a dictionary as well.

Is there something I can do with a (non-dictionary) object that I could never do with a dictionary? Or is it possible to set up a dictionary that completely looks like an object of a certain class?

This question is not about how they are created, but about how I can use them afterwards. Links or references are appreciated.

like image 226
Cephalopod Avatar asked Aug 19 '11 08:08

Cephalopod


People also ask

What is the difference between dictionary and object?

The data stored in the form of key-value pairs is called an Object or a Dictionary. Objects and dictionaries are similar; the difference lies in semantics. In JavaScript, dictionaries are called objects, whereas, in languages like Python or C#, they are called dictionaries.

What is the difference between objects stored in list and dictionary?

But what's the difference between lists and dictionaries? A list is an ordered sequence of objects, whereas dictionaries are unordered sets. However, the main difference is that items in dictionaries are accessed via keys and not via their position.

What is an dictionary in Python?

Dictionary. Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.


1 Answers

After an object has been created, I can add and remove slots at will, as I can do with a dictionary. Even methods are just objects stored in slots,

Be careful saying slots -- __slots__ has a specific meaning in Python.

so I probably can add methods to a dictionary as well.

foo = {} foo.bar = 1  AttributeError: 'dict' object has no attribute 'bar' 

Not by default, you'd need to subclass it -- but then you're using a class. Instead, put the function in the dictionary:

def bar():     return 1  foo['bar'] = bar foo['baz'] = lambda: 1 

Is there something I can do with an object that I could never do with a dictionary?

This question actually has a false premise -- dictionaries are objects, so anything you do with a dictionary you are doing with an object. For the rest of this answer, I'll pretend you mean "user defined class" when you say "object".

No, there is nothing you can do with a user-defined class you can't do with a dictionary. Classes are even implemented with a dictionary.

class Foo(object):     pass  print Foo.__dict__ # {'__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__',  #      '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None} 

Anything you can do with a class in Python you can do without one, with more work (and code which is much harder to understand, use, and maintain). It's even been said that Python classes are just syntactic sugar for dictionaries.

A very similar question was asked recently as Do I need to learn about objects, or can I save time and just learn dictionaries? I think my answer to that is also relevant here.

like image 126
agf Avatar answered Sep 24 '22 08:09

agf