Many newbie questions regarding python

Tim Harig usernet at ilthio.net
Fri Oct 8 02:09:14 CEST 2010


On 2010-10-07, Rogério Brito <rbrito at ime.usp.br> wrote:
> 1 - The first issue that I am having is that I don't seem to be able to, say,
> use something that would be common for people writing programs in C: defining a
> one-dimensional vector and only initializing it when needed.
>
> For instance, in C, I would write something like:
>
> int v[20];
> for (i = 0; i < 20; i++)
>     v[i] = 0;
>
> Note that I only define the vector v (and its size) at the beginning but
> initialize it latter during the code per-se.

You are reserving enough space to be defined as an array of 20 integers.
Note that after you declare it, you *can* access those memory locations
even before you initialize them.  They therefore still contain something
even if you have not yet defined what that something is.  Note that
uninitialized variables are a common C error.

Python is dynamic. You never need to allocate memory for something, you
simply assign something and the system takes care of the allocation for
you.  Before something is assigned, it cannot be addressed.  Nothing takes
up memory before it is assigned.

> Unfortunately, this doesn't work, as I get an index out of bounds when trying to
> index the v list. Of course, the main difference between the two snippets is
> that, in C, I declared v to have 20 positions, while in python I initialized it
> to be the empty list and, indeed, it has an empty set of indexes.
>
> What is the Pythonic way of writing code like this? So far, I have found many
> alternatives and I would like to write code that others in the Python community
> would find natural to read. Some of the things that crossed my mind:
>
>     v = [0 for i in range(20)]
>
>     v = [0] * 20
>
>     v = []
>     for i in range(20): v.append(0)
>
> What should I prefer? Any other alternative?

It really would help to know what you are trying to with v.  If we knew
that, we might be able to make better suggestions.  Usually in Python,
there is no need to initialize elements in a list as you can simply add
them as you need them.

> If possible, I would like to simply declare the list and fill it latter in my
> program, as lazily as possible (this happens notoriously when one is using a
> technique of programming called dynamic programming where initializing all
> positions of a table may take too much time in comparison to the filling of the
> array).

What I *think* you *might* be looking for is a kind of sparse list.  You
can achieve this by using a dictionary with numeric keys rather then a
list.  Not that when you do this, you recreate the C problem of trying to
access something that has not been allocated.  You either need to make sure
that a key exists before trying to access it or catch and handleKeyError.

> 2 - If I declare a class with some member variables, is is strictly necessary
> for me to qualify those members in a method in that class? For instance, if I
> define:
>
> class C:
>     f = 1
>     def g(self):
>         return f

Note that you have assigned f to the class object and not to the instance
objects.  This is a common pitfall for new Python programmers.  I suggest
assigning f in the constructor unless you are really sure that you want to
assign it to the class object which will be shared among the instance
objects.

> I get an annoying message when I try to call the g method in an object of type
> C, telling me that there's no global symbol called f. If I make g return self.f
> instead, things work as expected, but the code loses some readability.
>
> Is there any way around this or is that simply "a matter of life"?

Each member functions has its own scope.  The object itself is not part of
this scope.  Therefore, you must use the self object reference to access
other members of the object containing the function.

I don't really see the readability problem; but, you could work around it
by creating a reference to the member at the start of the function:
	
	def g(self):
		f = self.f

For this to work, f must be mutable.  If f is not mutable then you can wrap
f inside of a class in such a way that it is mutable.  Do this at your own
risk.  In doing this, you are starting to play with fire.



More information about the Python-list mailing list