Many newbie questions regarding python

Andreas Waldenburger usenot at geekmail.INVALID
Fri Oct 8 01:43:39 CEST 2010

On Thu, 07 Oct 2010 20:10:14 -0300 Rogério Brito <rbrito at>

> I am used to some languages like C, but I am just a complete newbie
> with Python and, while writing some small snippets, I had encountered
> some problems, with which I would sincerely appreciate any help,
> since I appreciate this language to write my "running pseudocode in"
> and I am seriously thinking of using it to teach some algorithms
> classes.
Let me congratulate you on your choice.

> 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.
> [snip]
I could be cheeky here and ask "Do you really need this? REALLY?". And
I will: Really? ;)

> Note that I only define the vector v (and its size) at the beginning
> but initialize it latter during the code per-se.
> [snip]
> 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)]
Pretty good.

>     v = [0] * 20
This will break on you if you replace the zeros with mutable objects.
Try it, you'll be unpleasantly surprised.

I guess it's OK for numbers, though. Or tuples, if need be.

>     v = []
>     for i in range(20): v.append(0)
That's the older (pre list comp) way, I guess. No need for it anymore,
unless the initialization is more complicated than this.

> 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).
Nah, don't worry too much about performance. You can do that when your
program is too slow.

I know that this isn't too satisfying, but seriously: Try to write
programs for humans, not for the computer. (I'm talking about Python
here; in C it's certainly par for the course to try to think like a
computer does.)

> 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
> 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.
No it doesn't.

(... pedagogical pause ...)

Python kind of forces you to be *very* explicit about which f you mean.
And this is a good thing. If you mean the global f, then say f. If you
mean the instance attribute of the current instance then say self.f and
if you mean the class attribute then say C.f (or, if you fear you're
going to be renaming C a lot, self.__class__.f).

Note here that self.f refers to the f as accessible by the
specific *instance* of the class, *not* the class attribute C.f. If you
don't change f on the instance, then they'll coincide, otherwise they
won't. Example (warning: not Python 3 compatible!):

[wildemar at localhost ~]$ ipython
Python 2.5.2 (r252:60911, Sep 30 2008, 15:41:38)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.4 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: class C:
   ...:     f = 1
   ...:     def g(self):
   ...:         return self.f

In [2]: c = C();

In [3]: print c.g()

In [4]: C.f = 2

In [5]: print c.g()

In [6]: c.f = 3

In [7]: print c.g()

In [8]: print C.f

> Is there any way around this or is that simply "a matter of life"?

> I have some other questions, but I will save them for latter.
Keep 'em coming. But be prepared for hearing "get used to it" many a
time. Or, from the more harsh folks, "don't try to write C in
Python". :)

> Please, keep in mind that I am a newbie in Python. Despite that, I am
> enjoying the little that I know.
Really? I never enjoy knowing just a little. ;)

To reach me via email, replace INVALID with the country code of my home 
country.  But if you spam me, I'll be one sour kraut.

More information about the Python-list mailing list