473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

error converting list to tuple

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
Jul 18 '05 #1
10 4715
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
Jul 18 '05 #2
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
Jul 18 '05 #3
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

Jul 18 '05 #4

"Stefan Seefeld" <se*****@sympatico.ca> wrote in message
news:a3********************@news20.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


Jul 18 '05 #5
Terry Reedy wrote:
"Stefan Seefeld" <se*****@sympatico.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

Jul 18 '05 #6
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_SetItem' 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.

Jul 18 '05 #7
Stefan Seefeld <se*****@sympatico.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_SetItem' 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
Jul 18 '05 #8
Thomas Heller wrote:
Replacing 'PyDict_SetItem' by 'PyObject_SetItem' 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_GetItem
and what they are doing byhind the scene ?

Regards,
Stefan

Jul 18 '05 #9
Stefan Seefeld <se*****@sympatico.ca> writes:
Thomas Heller wrote:
Replacing 'PyDict_SetItem' by 'PyObject_SetItem' 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_GetItem
and what they are doing byhind the scene ?


The PyObject_GetItem() 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
Jul 18 '05 #10
Thomas Heller wrote:
But back to the other issue: isn't there any text that explains
when I should be using PyDict_GetItem and when PyObject_GetItem
and what they are doing byhind the scene ?
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.


yeah, I did read that. The inconsistency (or even error, I would argue) is
in the interpretation of 'is a'. PyDict_Check reports 'true' when called
on my 'Dictionary' object, yet the PyDict_* API only applies to the
slice representing the base class. I don't think that is correct, it is
at least very surprising.

The API only supports two extremes:

* the object's type is an *exact* match for a built-in type, thus a highly
optimized API can be used to access its methods.

* the object's type is unknown, it may or may not support a certain 'protocol',
i.e. for all method invocations a full method lookup has to be done.

That sounds good for the pre 2.2 world, where built-in types and user-defined
types were distinct sets.

Regards,
Stefan
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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...
4
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...
3
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...
11
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')) ->
31
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
10
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...
5
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....
9
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), ... ] ...
0
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: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.