On Tue, Jul 25, 2006 at 08:08:32PM +0200, Sch?le Daniel wrote:
> [EMAIL PROTECTED] schrieb:
> >> cnt = 1
> >> def foo():
> >> global cnt
> >> cnt += 1
> >> return cnt
> >>
> >> def bar(x=foo()):
> >> print x
> >>
> >> bar() # 2
> >> bar() # 2
> >> bar() # 2
> >
> > Looks to me like you want to use the following programming pattern to
> > get dynamic default arguments:
> >
> > cnt = 1
> > def foo():
> > global cnt
> > cnt += 1
> > return cnt
> >
> > def bar(x=None):
> > if x is None:
> > x = foo()
> > print x
> >
> > bar() # 2
> > bar() # 3
> > bar() # 4
>
> yes, I haven't thought of that
> nowI changed my class to
>
> class Graph:
> settings = {
> "NumNodes" : 10,
> "MinNodes" : 2,
> "MaxNodes" : 5
> }
> def randomizeEdges(self,
> lowhigh = (settings["MinNodes"], settings["MaxNodes"])):
Note that this is a change in behaviour from what you originally stated you
wanted. settings is a dictionary that is shared between all instances of
Graph. The previous poster was suggesting the correct pattern for the
behaviour you requested. Generally if you want a parameter to have a dynamic
default argument you set the default value to None, test for None in the body
of the function/method and set the value appropriately.
> low, high = lowhigh
> for node in self.nodes:
> x = random.randint(low, high)
> # link the nodes
>
>
> maybe the only minor point is that no relationship
> can be expressed
>
> settings = {
> "NumNode" : 10,
> "MinNode" : settings["NumNode"] / 2,
> "MaxNode" : settings["NumNode"]
> }
>
> but I think the solution is nevertheless ok
>
>
> Regards, Daniel
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list