Python Forum
function 2 inside function 1 parameters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function 2 inside function 1 parameters
I know there is an import for this but i forgot it. One of the parameters in my function requires a function. I'll try and say this more simply. Main executes function1. One of the parameters for function1 is another function, function2. My problem is the function is launching before straight out of the paramters. I can't put no parenthesis because function2 has parameters. TIA for your help.
It is very difficult to understand. Can you post some code explaining the issue? Functions are ordinary python objects, they can be used like any other type of parameters.
I have class shop. When making the __init__ I need to give it a function within one of the parameter player. If I give it the function though, the function executes from within the parameter of the class shop, rather than executing when the shop class executes it

def openCabin(p):
    health = getattr(p, 'damageTaken')
    healthSub = health - (health * 2)

Cabin = Building(260, 30, 200, 200, Buildings[1], openCabin(p))
The function executes one line 9 where it says "openCabin"
#4 Wrote:functools.partial(func, *args, **keywords)
  • Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords. If more arguments are supplied to the call, they are appended to args. If additional keyword arguments are supplied, they extend and override keywords.

from functools import partial
Cabin = Building(260, 30, 200, 200, Buildings[1], partial(openCabin, p=p))
Another way give only in openCabin when instantiate the Cabin object.
class Bar:
    def __init__(self, open_cabin):
        self.open_cabin = open_cabin

def open_cabin(p):
    return p
>>> cabin = Bar(open_cabin)
>>> cabin.open_cabin
<function open_cabin at 0x0324B4B0>
>>> # First now will executes the open_cabin function with a argument
>>> cabin.open_cabin(42)

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make global list inside function CHANKC 6 218 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Parameters aren't seen inside function Sancho_Pansa 8 253 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Passing argument from top-level function to embedded function JaneTan 2 196 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 531 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Function parameters and values as string infobound 1 275 Jul-24-2020, 04:28 AM
Last Post: scidam
  use NULL as function parameter which is a pointer function? oyster 0 288 Jul-11-2020, 09:02 AM
Last Post: oyster
  How to use function result in another function Ayckinn 4 425 Jun-16-2020, 04:50 PM
Last Post: Ayckinn
  Creating function inside classes using type wolfmansbrother 3 520 Mar-20-2020, 01:21 AM
Last Post: wolfmansbrother
  Custom function that prints function text gyaan_anveyshak 3 517 Jan-28-2020, 05:43 AM
Last Post: perfringo
  Unrelated new function causing existing working function to fail Nick_G 2 622 Jan-27-2020, 07:21 PM
Last Post: Nick_G

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020