Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

python - How do you call a function in a function?

I have a function and I'm making another one in which I need to call the first function. I don't have experience in Python, but I know that in languages like MATLAB it's possible as long as they're in the same directory.

A basic example:

def square(x):
    square = x * x

(and saved)

Now in my new function I want to use the function square I tried:

def something (y, z)
  import square
  something = square(y) + square(z)
  return something

Which displays: builtins.TypeError: 'module' object is not callable.

What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No need for import if its in the same file.

Just call square from the something function.

def square(x):
  square = x * x
  return square

def something (y, z)
  something = square(y) + square(z)
  return something

More simply:

def square(x):
  return x * x

def something (y, z)
  return square(y) + square(z)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...