469,955 Members | 2,157 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Is it allowed to use function results as default arguments ?

hello,

I've a perfect working procedure,
at least as far I've tested it it works perfect.

But I was just experimenting with inspect,
and saw that the default argument was not parsed correctly.

So I wonder if this is allowed:

def Get_Relative_Path ( target, base=os.curdir ) :
...

As inspect returns the following:

(['target', 'base'], None, None, ('.',))

thanks,
Stef Mientki
Jul 28 '08 #1
5 841
On Jul 28, 1:28*pm, Stef Mientki <stef.mien...@gmail.comwrote:
hello,

I've a perfect working procedure,
at least as far I've tested it it works perfect.

But I was just experimenting with inspect,
and saw that the default argument was not parsed correctly.

So I wonder if this is allowed:

def Get_Relative_Path ( target, base=os.curdir ) :
* ...

As inspect returns the following:

(['target', 'base'], None, None, ('.',))

thanks,
Stef Mientki
os.curdir is '.' on many platforms. What did you expect inspect to
show?

|>>import os
|>>os.curdir
'.'
Jul 28 '08 #2
On Jul 28, 3:28*pm, Stef Mientki <stef.mien...@gmail.comwrote:
hello,

I've a perfect working procedure,
at least as far I've tested it it works perfect.

But I was just experimenting with inspect,
and saw that the default argument was not parsed correctly.

So I wonder if this is allowed:

def Get_Relative_Path ( target, base=os.curdir ) :
Did you perhaps mean to say def Get_Relative_Path(target,
base=os.getcwd()):
* ...

As inspect returns the following:

(['target', 'base'], None, None, ('.',))

thanks,
Stef Mientki
Jul 29 '08 #3
Well, others have answered the question, but I thought I'd throw in
that it would be more pythonic to do something like:

def Get_Relative_Path(target, base = None):
if base is None:
base = os.curdir
...

Jul 30 '08 #4
"fred.haab" <fr*******@gmail.comwrites:
Well, others have answered the question, but I thought I'd throw in
that it would be more pythonic to do something like:

def Get_Relative_Path(target, base = None):
if base is None:
base = os.curdir
...
Even more Pythonic would be to name the function by the style guide
(PEP 8):

def get_relative_path(target, base=None):
if base is None:
base = os.curdir
# …

--
\ “If trees could scream, would we be so cavalier about cutting |
`\ them down? We might, if they screamed all the time, for no good |
_o__) reason.” —Jack Handey |
Ben Finney
Jul 30 '08 #5


fred.haab wrote:
Well, others have answered the question, but I thought I'd throw in
that it would be more pythonic to do something like:

def Get_Relative_Path(target, base = None):
if base is None:
base = os.curdir
...
Since os.curdir is a constant, this is nonesensical. One only needs the
dummy default when one wants an expression re-evaluated with each call.

Jul 30 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by laredotornado | last post: by
3 posts views Thread by Beta What | last post: by
7 posts views Thread by andrewfsears | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.