This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Dive into Python

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Chapter 3: Native Datatypes. Due 05/01/11

Go back to: General discussion

This thread is for comments/questions about Chapter 3, Native Datatypes.

John Lowe's picture
John Lowe
Wed, 2011-04-27 16:42

What is due for this Chapter? Is there an assignment or question set we complete or just read and discuss?

M. Volz's picture
M. Volz
Wed, 2011-04-27 22:56

Yup, just read and discuss. As you're doing so you (obviously) should try out what we're learning.

justin stoller's picture
justin stoller
Mon, 2011-05-02 03:06

I liked the chapter, though I didn't really see much to talk about with the basic data types. I've done some Ruby coding and I was interested in the idea of Tuples, we don't have those in Ruby. Are they that much faster than Lists?

It mentioned that you could have Tuples of Lists. I assumed that Tuples were something more like C arrays, but how can you have a static array of variable length elements in that kind of scheme?

M. Volz's picture
M. Volz
Wed, 2011-05-04 17:20

A tuple is actually a 1-d data structure; it simply stores the memory location of the list. The memory location of the list is immutable, but the contents of the list can change if you have a handle to it.

For example:

sample_list = [1,"2","b"]
sample_tuple = (sample_list,"h",5)
print sample_tuple
print sample_list.pop()
print sample_list
print sample_tuple

gives output

([1, '2', 'b'], 'h', 5)
b
[1, '2']
([1, '2'], 'h', 5)

This does not seem to work for storing more primitive datatypes. For instance, the output of this

some_variable = "fish"
sample_tuple = (some_variable)
print sample_tuple
some_variable = "tuna"
print sample_tuple

is

fish
fish

This is what I expected since many languages function this way- for example, Java also stores memory location for objects, but stores the value for primitive data types. In fact, this property has lead people to call Java "pass by reference" for objects and "pass by value" for primitive types, but technically that phrase has a different meaning, and if I told you that about Python it would make the compiler guys mad at me, see: http://javadude.com/articles/passbyvalue.htm