Is it allowed to use function results as default arguments ?

S

Stef Mientki

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
 
S

Simon Forman

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
'.'
 
B

Benjamin

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
 
F

fred.haab

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
...
 
T

Terry Reedy

fred.haab said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top