Re: self question
- From: "Mike" <mike@xxxxxxxxx>
- Date: 26 Jul 2006 11:03:33 -0700
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
.
- Follow-Ups:
- Re: self question
- From: Duncan Booth
- Re: self question
- References:
- self question
- From: Schüle Daniel
- self question
- Prev by Date: Re: Need a compelling argument to use Django instead of Rails
- Next by Date: Re: Threads vs Processes
- Previous by thread: Re: self question
- Next by thread: Re: self question
- Index(es):
Relevant Pages
|
|