Re: self question




Schüle Daniel wrote:
Hi all,

given python description below

import random

class Node:
def __init__(self):
self.nachbarn = []

class Graph(object):
# more code here
def randomizeEdges(self, low=1, high=self.n):
pass


graph = Graph(20)
graph.randomizeEdges(2,5)

I am burned by high=self.n
quick test with

cnt = 1
def foo():
global cnt
cnt += 1
return cnt

def bar(x=foo()):
print x

bar() # 2
bar() # 2
bar() # 2

this is not behaviour C++ programmer would expect
does someone know why this kind of behaviour is/was choosen?

Regards, Daniel

I think the answer is that 'def' is an executable statement in python
rather than a definition that the compiler interprets at compile time.

As a result the compiler can evaluate 'foo()' when it defines 'bar', so
it does.

The following works as expected:
def bar():
print foo()

Hopefully somebody more knowledgable will also respond

.



Relevant Pages

  • Advice on this code
    ... def wrkpol: ... while cnt < maxcnt: ... # The following code creates a connection object, ...
    (comp.lang.python)
  • self question
    ... given python description below ... def foo: ... global cnt ...
    (comp.lang.python)
  • Re: self question
    ... def foo: ... global cnt ... Looks to me like you want to use the following programming pattern to ...
    (comp.lang.python)
  • Re: Class property
    ... This now cannot be implemented with a metaclass, because the metaclass cannot operate on the class attributes: ... cnt = 0 ... def __init__: ... File "", line 11, in ratio ZeroDivisionError: float division py> b = B ...
    (comp.lang.python)
  • Re: Unable to abort a FTP command?
    ... so it won't break the recv loop and close data connection. ... getpart and callback like this, ... def getpart(self, ftp_filename, rest, cnt, out_filename): ...
    (comp.lang.python)