Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between Python Dictionaries vs Javascript Objects?

I'm new to python and I was reading about Dictionaries. And from my previous experience with langages like javascript they seemed like objects to me. Dictionaries can store lists and share many similaraties to objects in javascript.

ex python code:

menu = {} menu['Chicken Alfredo'] = 14.50 menu['Italian Pasta'] = 15.89 menu['Shrimp Soup'] = 12.43 menu['Persian Rice'] = 21.99 

ex javascript code:

var menu = new Object(); menu['Chicken Alfredo'] = 14.50; menu['Italian Pasta'] = 15.89; menu['Shrimp Soup'] = 12.43; menu['Persian Rice'] = 21.99; 

What's the difference here, they both do the same job, but there different concepts?

like image 645
Ragnar Avatar asked Jan 08 '14 05:01

Ragnar


People also ask

Are JavaScript objects same as Python dictionaries?

In Javascript, a dictionary is the same as an object. It can be initialized using the same syntax as Python. The key can be a number, a string, or an identifier.

What is the difference between a dictionary and an object in Python?

A dictionary is an arbitrary mapping. An object is a special mapping from names to variables and methods. A class is a language construct that gathers together objects with similar structure and helps to create objects. Objects and classes can be simulated in a straightforward way using functions and dictionaries.

Are JavaScript objects like dictionaries?

Are there dictionaries in JavaScript? No, as of now JavaScript does not include a native “Dictionary” data type. However, Objects in JavaScript are quite flexible and can be used to create key-value pairs. These objects are quite similar to dictionaries and work alike.

Does Python have objects like JavaScript?

Python's "object-based" subset is roughly equivalent to JavaScript. Like JavaScript (and unlike Java), Python supports a programming style that uses simple functions and variables without engaging in class definitions.


1 Answers

From :

In Python, dictionaries are a form of mapping type. They can be initialized using a sequence of comma-separated name: value pairs, enclosed in curly braces. They are accessed using array notation involving square braces. The key can be any hashable, including numbers and strings.

In Javascript, a dictionary is the same as an object. It can be initialized using the same syntax as Python. The key can be a number, a string, or an identifier. Because the dictionary is also an object, the elements can be accessed either using array notation, e.g. b[i], or using property notation, e.g. b.i.

Consider an identifier used in an initializer, such as

 b = {i:j}  

In Python both i and j are evaluated, but in Javascript, only j is evaluated. In Javascript you also have the privilege of writing in the dot notation, using the identifier i. Hence in Python,

 i='k'  j=1  b = {i:j}  b['k'] # -> 1  

In Javascript,

 i='k'  j=1  b = {i:j}  b['i'] // -> 1  b.i // -> 1  // b[i], b['k'] and b.k are not defined  

In Javascript, using the identifier in dot notation is completely identical in all cases to using a string that "looks like" the identifier in array notation. Hence, b = { 'i':1 } ; b['i'] // -> 1 b.i // -> 1 When a number or boolean is used in a dictionary, Javascript will access the element using a string representation of the number or boolean. Not so in Python — a string and a number (or boolean) are different hashables.

If you are interested in differences between both languages, then look at ans

like image 164
a.m. Avatar answered Sep 19 '22 14:09

a.m.