How would __getitem__ & __setitem__ be implemented in python code? [on hold]
Ok so i'm wondering how __getitem__
and __setitem__
would be implemented in Python code?
I've googled and searched and can't find how it really works internally, this is from me wanting to know how a Python dict
actually works and is implemented and I have learned how to do that in Python with hash-table using lists in list for slots and chaining for collisions etc I found it all very interesting...
but now I would like to go deeper into how the above magic methods actually are implemented.... what would the code look like to create that magic methods?
python methods
put on hold as off-topic by Mad Physicist, pault, arghtype, Rob, Patrick Mevzek Dec 28 '18 at 0:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Mad Physicist, pault, arghtype, Rob, Patrick Mevzek
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
Ok so i'm wondering how __getitem__
and __setitem__
would be implemented in Python code?
I've googled and searched and can't find how it really works internally, this is from me wanting to know how a Python dict
actually works and is implemented and I have learned how to do that in Python with hash-table using lists in list for slots and chaining for collisions etc I found it all very interesting...
but now I would like to go deeper into how the above magic methods actually are implemented.... what would the code look like to create that magic methods?
python methods
put on hold as off-topic by Mad Physicist, pault, arghtype, Rob, Patrick Mevzek Dec 28 '18 at 0:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Mad Physicist, pault, arghtype, Rob, Patrick Mevzek
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
1
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05
|
show 2 more comments
Ok so i'm wondering how __getitem__
and __setitem__
would be implemented in Python code?
I've googled and searched and can't find how it really works internally, this is from me wanting to know how a Python dict
actually works and is implemented and I have learned how to do that in Python with hash-table using lists in list for slots and chaining for collisions etc I found it all very interesting...
but now I would like to go deeper into how the above magic methods actually are implemented.... what would the code look like to create that magic methods?
python methods
Ok so i'm wondering how __getitem__
and __setitem__
would be implemented in Python code?
I've googled and searched and can't find how it really works internally, this is from me wanting to know how a Python dict
actually works and is implemented and I have learned how to do that in Python with hash-table using lists in list for slots and chaining for collisions etc I found it all very interesting...
but now I would like to go deeper into how the above magic methods actually are implemented.... what would the code look like to create that magic methods?
python methods
python methods
edited Dec 27 '18 at 14:59
Ulrich Eckhardt
12.5k11536
12.5k11536
asked Dec 27 '18 at 14:45
n c
1015
1015
put on hold as off-topic by Mad Physicist, pault, arghtype, Rob, Patrick Mevzek Dec 28 '18 at 0:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Mad Physicist, pault, arghtype, Rob, Patrick Mevzek
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Mad Physicist, pault, arghtype, Rob, Patrick Mevzek Dec 28 '18 at 0:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Mad Physicist, pault, arghtype, Rob, Patrick Mevzek
If this question can be reworded to fit the rules in the help center, please edit the question.
2
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
1
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05
|
show 2 more comments
2
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
1
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05
2
2
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
1
1
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05
|
show 2 more comments
1 Answer
1
active
oldest
votes
If you want to learn how python dicts work, the best resource you'll find is this talk by Raymond Hettinger (one of the main python programmers). For a simple example of getitem and setitem, here is a point class that uses them correctly.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, ind):
if ind<0 or ind>2:
raise IndexError()
return x if ind == 0 else y
def __setitem__(self, ind, val)
if ind == 0:
self.x = val
elif ind == 2:
self.y = val
raise IndexError()
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to learn how python dicts work, the best resource you'll find is this talk by Raymond Hettinger (one of the main python programmers). For a simple example of getitem and setitem, here is a point class that uses them correctly.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, ind):
if ind<0 or ind>2:
raise IndexError()
return x if ind == 0 else y
def __setitem__(self, ind, val)
if ind == 0:
self.x = val
elif ind == 2:
self.y = val
raise IndexError()
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
|
show 3 more comments
If you want to learn how python dicts work, the best resource you'll find is this talk by Raymond Hettinger (one of the main python programmers). For a simple example of getitem and setitem, here is a point class that uses them correctly.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, ind):
if ind<0 or ind>2:
raise IndexError()
return x if ind == 0 else y
def __setitem__(self, ind, val)
if ind == 0:
self.x = val
elif ind == 2:
self.y = val
raise IndexError()
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
|
show 3 more comments
If you want to learn how python dicts work, the best resource you'll find is this talk by Raymond Hettinger (one of the main python programmers). For a simple example of getitem and setitem, here is a point class that uses them correctly.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, ind):
if ind<0 or ind>2:
raise IndexError()
return x if ind == 0 else y
def __setitem__(self, ind, val)
if ind == 0:
self.x = val
elif ind == 2:
self.y = val
raise IndexError()
If you want to learn how python dicts work, the best resource you'll find is this talk by Raymond Hettinger (one of the main python programmers). For a simple example of getitem and setitem, here is a point class that uses them correctly.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, ind):
if ind<0 or ind>2:
raise IndexError()
return x if ind == 0 else y
def __setitem__(self, ind, val)
if ind == 0:
self.x = val
elif ind == 2:
self.y = val
raise IndexError()
edited Dec 27 '18 at 14:59
answered Dec 27 '18 at 14:55
Oscar Smith
406616
406616
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
|
show 3 more comments
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
There's no "throw" in Python. And I fail to see how this answers the question actually...
– bruno desthuilliers
Dec 27 '18 at 14:56
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
I know how to use getitem and setitem correctly, i want to know how its written/works in python / how you would go about writing that edit: method in python
– n c
Dec 27 '18 at 14:57
i mean method, sorry
– n c
Dec 27 '18 at 15:00
i mean method, sorry
– n c
Dec 27 '18 at 15:00
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
do you mean specifically for a dict? or are you wondering how * methods get hooked?
– Oscar Smith
Dec 27 '18 at 15:01
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
i guess in general yea, how would you go about creating a method that can do the same as getitem in python code? and allow you to use brackets etc like it does
– n c
Dec 27 '18 at 15:04
|
show 3 more comments
2
Here is the latest version of the CPython implementation of the whole class: github.com/python/cpython/blob/master/Objects/dictobject.c. the code comments are quite illuminating.
– Mad Physicist
Dec 27 '18 at 14:51
1
Note that those methods are not specific to dicts - lists have them both too, tuples and strings have the first but not the second, and actually just any class can implement one or both of those methods as it sees fit.
– bruno desthuilliers
Dec 27 '18 at 14:55
yea i understand its not specific to dicts, i was just using that as an example as that's were the curiosity came from, in finding out how dicts work..now i know how they work inside, i'm wondering how these two magic methods work if you get me? and i cant find any info anywhere online
– n c
Dec 27 '18 at 15:07
You may want to look at the Python Data Model, particularly (but not only) the section on emulating container types
– snakecharmerb
Dec 27 '18 at 15:48
maybe what I'm asking cant be answered and maybe its the python interpreter that translates the syntax to such?
– n c
Dec 27 '18 at 16:05