PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 26, 2022

[FIXED] How to reuse a function within a python module

 November 26, 2022     function, module, python     No comments   

Issue

Im trying to reuse a function within another function of a Python module im writing. My search only reveals the same tutorial over and over again on how to write and use simple modules.

What I'm trying to do:

module.py:

def func1(num1, num2):
    return num1 + num2

def func2(num1, num2, num3):
    return func1(num1, num2) * num3

main.py

import module as mod

num1 = 1
num2 = 2
num3 = 3

res = mod.func2(num1, num2, num3)

But this throws a NameError: NameError: name 'func1' is not defined

I think this happens because func1 from module.py has not been loaded in the kernel, but I don't know how to solve this without using from module import * which I really want to avoid.

Is there a way to reference func1 from within func2? Or any other way to specify, that func2 should load and use func1?


Solution

I tried this and it worked without any problems with Ubuntu default Python 2.7

from module import func2
num1 = 1
num2 = 2
num3 = 3
res = func2(num1, num2, num3)
res
9


Answered By - Gadi
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing