hi there,
I'm trying to convert a tuple to a list,
and get a 'TypeError: list objects are unhashable'.
Can anybody enlighten me as to the possible causes
for this ? Where does hashing come into play during
this conversion ?
Could it be that my runtime is corrupted ?
The code is executed from within a C++ extension
module I'm developing...
Thanks for any help,
Stefan
PS: I'm using python 2.3.3 in case that matters 10 4741
Stefan Seefeld wrote: hi there,
I'm trying to convert a tuple to a list, and get a 'TypeError: list objects are unhashable'.
Can anybody enlighten me as to the possible causes for this ? Where does hashing come into play during this conversion ?
Could it be that my runtime is corrupted ? The code is executed from within a C++ extension module I'm developing...
Thanks for any help, Stefan
PS: I'm using python 2.3.3 in case that matters
God I can't believe I think I can answer this question:
mylist=[]
t=('123','hello ','bye')
for count in range(0,len(t)) :
mylist.append(t[count])
It works for me.
John
john fabiani wrote: Stefan Seefeld wrote:
hi there,
I'm trying to convert a tuple to a list, and get a 'TypeError: list objects are unhashable'.
Can anybody enlighten me as to the possible causes for this ? Where does hashing come into play during this conversion ?
Could it be that my runtime is corrupted ? The code is executed from within a C++ extension module I'm developing...
Thanks for any help, Stefan
PS: I'm using python 2.3.3 in case that matters
God I can't believe I think I can answer this question: mylist=[] t=('123','hello ','bye') for count in range(0,len(t)) : mylist.append(t[count])
It works for me. John
even better than above:
mylist=list(t)
See everyone I'm learning but it is not easy for an old man to learn new
tricks.
John
john fabiani wrote: Stefan Seefeld wrote:
hi there,
I'm trying to convert a tuple to a list, and get a 'TypeError: list objects are unhashable'.
Can anybody enlighten me as to the possible causes for this ? Where does hashing come into play during this conversion ?
Could it be that my runtime is corrupted ? The code is executed from within a C++ extension module I'm developing...
Thanks for any help, Stefan
PS: I'm using python 2.3.3 in case that matters
God I can't believe I think I can answer this question:
well, unfortunately that wasn't my question <wink/>
I do:
t = ('hello',)
l = list(t)
which is the standard tuple->list conversion. And yes,
it does work when I run that code in isolation.
The point is that the above, as simple as it looks,
fails with said exception in a very specific context
(I call this code from within a C++ extension module),
so I'm wondering whether I could have messed up the
python runtime somehow or whether there are other
possible explanations for the exception.
Regards,
Stefan
"Stefan Seefeld" <se*****@sympat ico.ca> wrote in message
news:a3******** ************@ne ws20.bellglobal .com... (I call this code from within a C++ extension module),
You should have said that in the beginning since that is a whole different
ball game: extension modules in C are not Python.
so I'm wondering whether I could have messed up the python runtime somehow or whether there are other possible explanations for the exception.
It is easy to mess up in C ;-). You are most likely to get an answer (from
another C extension writer) if you post a short as possible code snippet
that exhibits the error, and the actual and complete error message.
tjr
Terry Reedy wrote: "Stefan Seefeld" <se*****@sympat ico.ca> wrote in message
(I call this code from within a C++ extension module),
You should have said that in the beginning since that is a whole different ball game: extension modules in C are not Python.
ok, let me be more specific: From my C++ code I call a method
of a python object which happens to do the tuple->list conversion.
And as Python is implemented in C, I thought it didn't matter,
it's the same underlying API / object model, just different syntax ;-)
It is easy to mess up in C ;-). You are most likely to get an answer (from another C extension writer) if you post a short as possible code snippet that exhibits the error, and the actual and complete error message.
That's (part of) the problem: I can't reproduce this in isolation,
so I wanted to know how I could debug this.
What I do is calling 'PyObject_Str' on the python object. That object
has its '__str__' method implemented in a way that involves said
tuple->list conversion, so the call raises an exception and
the call returns 0.
Thanks,
Stefan
Terry Reedy wrote: It is easy to mess up in C ;-). You are most likely to get an answer (from another C extension writer) if you post a short as possible code snippet that exhibits the error, and the actual and complete error message.
I found the problem, and I'd like to share my new insight, as I don't
think it is obvious:
I hold an object reference that is a dictionary, and I modify it with
the PyDict_* family of functions.
However, the real object is not a builtin 'dict', but instead it is
derived from it:
class Dictionary(dict ):
...
I just realized that PyDict_SetItem (et al.) don't lookup the
methods in the object's __dict__, so the method that gets really
executed is not the overloaded I wrote, but the method from the
base class.
Replacing 'PyDict_SetItem ' by 'PyObject_SetIt em' did the trick.
I guess 'PyDict_SetItem ' is a shortcut to circumvent the lengthy
method lookup. Fair enough, but the python C API documentation could
be a bit more clear about that...
Kind regards,
Stefan
PS: for the curious: the missing link between the dict and
the list / tuple issue is that I derived my own dictionary
precisely to be able to use a list as key.
Stefan Seefeld <se*****@sympat ico.ca> writes: Terry Reedy wrote:
It is easy to mess up in C ;-). You are most likely to get an answer (from another C extension writer) if you post a short as possible code snippet that exhibits the error, and the actual and complete error message.
I found the problem, and I'd like to share my new insight, as I don't think it is obvious:
I hold an object reference that is a dictionary, and I modify it with the PyDict_* family of functions.
However, the real object is not a builtin 'dict', but instead it is derived from it:
class Dictionary(dict ): ...
I just realized that PyDict_SetItem (et al.) don't lookup the methods in the object's __dict__, so the method that gets really executed is not the overloaded I wrote, but the method from the base class.
Replacing 'PyDict_SetItem ' by 'PyObject_SetIt em' did the trick. I guess 'PyDict_SetItem ' is a shortcut to circumvent the lengthy method lookup. Fair enough, but the python C API documentation could be a bit more clear about that...
I would guess the *real* problem is that you are ignoring the return
value of PyDict_SetItem (or whatever api you use). In this case you get
weird errors afterwards.
Thomas
Thomas Heller wrote: Replacing 'PyDict_SetItem ' by 'PyObject_SetIt em' did the trick. I guess 'PyDict_SetItem ' is a shortcut to circumvent the lengthy method lookup. Fair enough, but the python C API documentation could be a bit more clear about that...
I would guess the *real* problem is that you are ignoring the return value of PyDict_SetItem (or whatever api you use). In this case you get weird errors afterwards.
good shot ! Well, what the 'real' problem is is probably a matter
of perspective, but you are quite right in that I failed to check
a return value (and associated exception) so python's own code
eventually bumped into it in an unrelated context.
But back to the other issue: isn't there any text that explains
when I should be using PyDict_GetItem and when PyObject_GetIte m
and what they are doing byhind the scene ?
Regards,
Stefan
Stefan Seefeld <se*****@sympat ico.ca> writes: Thomas Heller wrote:
Replacing 'PyDict_SetItem ' by 'PyObject_SetIt em' did the trick. I guess 'PyDict_SetItem ' is a shortcut to circumvent the lengthy method lookup. Fair enough, but the python C API documentation could be a bit more clear about that... I would guess the *real* problem is that you are ignoring the return value of PyDict_SetItem (or whatever api you use). In this case you get weird errors afterwards.
good shot ! Well, what the 'real' problem is is probably a matter of perspective, but you are quite right in that I failed to check a return value (and associated exception) so python's own code eventually bumped into it in an unrelated context.
And that will always catch you!
But back to the other issue: isn't there any text that explains when I should be using PyDict_GetItem and when PyObject_GetIte m and what they are doing byhind the scene ?
The PyObject_GetIte m() function belongs to the Abstract Objects Layer.
<http://docs.python.org/api/abstract.html>
The docs say:
6. Abstract Objects Layer
The functions in this chapter interact with Python objects regardless
of their type, or with wide classes of object types (e.g. all
numerical types, or all sequence types). When used on object types for
which they do not apply, they will raise a Python exception.
while PyDict_GetItem( ) belongs to the Concrete Objects Layer for
PyDictObject <types http://docs.python.org/api/concrete.html>:
7. Concrete Objects Layer
The functions in this chapter are specific to certain Python object
types. Passing them an object of the wrong type is not a good idea; if
you receive an object from a Python program and you are not sure that
it has the right type, you must perform a type check first; for
example, to check that an object is a dictionary, use
PyDict_Check(). The chapter is structured like the ``family tree'' of
Python object types.
Reading the include files may also help - once you get the idea it's simple.
Thomas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Yomanium Yoth Taripoät II |
last post by:
HI,
1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?
thx.
|
by: GrelEns |
last post by:
hello,
i wonder if this possible to subclass a list or a tuple and add more
attributes ? also does someone have a link to how well define is own
iterable object ?
what i was expecting was something like :
>>> t = Test('anAttributeValue', )
>>> t.anAttribute
|
by: Rakesh |
last post by:
In my Python code fragment, I want to write a code fragment such that
the minimum element of a tuple is subtracted from all the elements of a
given
tuple.
When I execute the following python script I get the following
interpreter
error.
|
by: vdavidster |
last post by:
Hello everyone,
I want to convert a tuple to a list, and I expected this behavior:
list(('abc','def')) ->
list(('abc')) ->
But Python gave me this behavior:
list(('abc','def')) ->
|
by: metiu uitem |
last post by:
Say you have a flat list:
How do you efficiently get
, , ]
I was thinking of something along the lines of:
for (key,number) in list:
print key, number
| |
by: robin |
last post by:
hi,
i'm doing some udp stuff and receive strings of the form '0.870000
0.250000 0.790000;\n'
what i'd need though is a list of the form
i got to the part to obtain a string '0.870000 0.250000
0.790000' but i can't find a way to convert this into a list. i tried
eval() but this gives me the following error:
Traceback (most recent call last):
|
by: metaperl.etc |
last post by:
The following program does not work if you uncomment #lis =
+ list(args)
Evidently Python is opting for the nullary constructor list() as
opposed to the other one which takes a sequence. But no newcomer would
know this. And the Python docs dont give a good example of dealing with
taking a sequence of args and converting it to a list.
I must've spent 40 minutes looking through my ora books, googling and
irc'ing in vane on this.
|
by: Steven Bethard |
last post by:
I have some text and a list of Element objects and their offsets, e.g.::
... (etree.Element('a'), 0, 21),
... (etree.Element('b'), 11, 18),
... (etree.Element('c'), 18, 18),
... ]
I'd like to produce the corresponding ElementTree. So I want to write a
get_tree() function that works like::
|
by: Gabriel Ibanez |
last post by:
Hi all ..
I'm trying to using the map function to convert a tuple to a list, without
success.
I would like to have a lonely line that performs the same as loop of the
next script:
-------------------------------------------
# Conveting tuple -list
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |