A bundle of questions from a Python newbie
Sean 'Shaleh' Perry
shalehperry at attbi.com
Wed Feb 20 23:56:15 CET 2002
> 1. The way I thought that an assignment like
> var += 1
> worked was that the interpreter would go to the place pointed by var and
> add 1. In particular, id(var) should remain constant. But this is not
> what happens as a simple test shows it. So what is wrong in my
> perception of what += does? Is this an implemetation issue?
without looking at the Python code, my assumption is that a new variable is
created for each call to '='.
> 4. I have read the FAQ about the "self controversy" - why one has to
> declare it as an argument and then use it explicitly in the body of the
> method - but I really feel that there must be a better solution. I
> thought something like this could be useful:
> a. self doesn't need to be declared.
> b. Distinguishing data members from local variables could be achieved by
> instead of using self.whatever use .whatever - a single dot in front of
> the name "whatever".
> I don't expect that this solution is adopted (it would break all the
> existing code - ahhh every Nero-like language designer's dream - wreak
> havoc in the citadel of its users and start afresh), what I would like
> to know is how bad/good is this idea.
the problem is the period in front of the word is VERY easy to miss.
Especially after a long day of coding or for those with poor eye sight.
> 6. Is there a reason why 3.__abs__() does not work? After all 3 is an
> object (in the wider sense of the word). And it does work if we assign
> it to a variable, eg,
> var = 3
> I know that code like the above (methods applied to number literals) is
> probably rare, but consistence is also a good thing.
because not everything is an object (yet)
> 10. I have a class that works like a list in the sense that supports the
> method __getitem__ and its relatives. But I'm at a loss as to what I
> have to do to support the slice notation.
>>> class Foo:
... def __getslice__(*args):
... print args
>>> f = Foo()
(<__main__.Foo instance at 0x80905e4>, 1, 1)
More information about the Python-list