[Tutor] pass tuples to user defined function(beginner)

Wayne Werner waynejwerner at gmail.com
Tue Nov 29 23:12:41 CET 2011


On Tue, Nov 29, 2011 at 3:40 PM, Mayo Adams <mayoadams at gmail.com> wrote:

>  I cant immediately see how anything in a script is
> anything other than a representation of some kind, hence the
> distinction between representamen and object does no work for me.
>

That's exactly it - when you have something like this in a file named, say
data.txt:

("This", 'is', 'a', 'silly', 'tuple')

Then that's all it is - just text data. I happens to *look* like a tuple,
but it's not. If you rename that file to data.py... well, it's still text
data. If you add an assignment operation to the line:

data = ("This", 'is', 'a', 'silly', 'tuple')

Then you could actually import it with `from data import data`. Once the
data is actually evaluated, then it's "live" - it really is a tuple, and it
can be used in tuple-ish ways.

I think the main reason to make a distinction like that is that if you
encounter a .py file with data like this:

> "__import__('os').system('echo i got you now rm-rf')"

You know it's probably going to do some bad things to your system if you
run it. If you open that in a text editor, on the other hand, it's unlikely
to do anything painful.

I think there's also some use in making the distinction between data and
code. Python provides you with /ridiculously/ easy ways to read in data. So
if you have a bunch of strings that you want in a tuple you should do
something like:

data = tuple(f.open('data.txt').readlines())

Which has the added advantage of *just* reading in strings. If you want to
convert that data into other types (integer, float, etc.) then you can
easily do it explicitly, and explicit is better than implicit.

In reality of course, all the data is just 1's and 0's floating out there,
but I think it's helpful to develop these abstractions.
HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111129/6e669bcb/attachment.html>


More information about the Tutor mailing list