Bottom Page

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.
Age Doesn't Matter, Your Passion Does
- Shreya Jain
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"
Age Doesn't Matter, Your Passion Does
- Shreya Jain
#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))
snippsat likes this post
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)

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Custom function that prints function text gyaan_anveyshak 3 159 Jan-28-2020, 05:43 AM
Last Post: perfringo
  Unrelated new function causing existing working function to fail Nick_G 2 262 Jan-27-2020, 07:21 PM
Last Post: Nick_G
  Animate graph nodes inside a function adamG 0 198 Sep-23-2019, 11:18 AM
Last Post: adamG
  Conditional importing inside a function Roger 1 698 Jul-03-2019, 10:02 PM
Last Post: Gribouillis
  using output of one function into another function ibaad1406 7 574 Jun-24-2019, 08:43 AM
Last Post: noisefloor
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 549 May-11-2019, 03:18 AM
Last Post: keames
  Parameters for function in bind() kom2 1 342 Apr-18-2019, 03:59 PM
Last Post: woooee
  AssertionError: View function mapping is overwriting an existing endpoint function Zhavi221 7 4,348 Apr-17-2019, 01:07 PM
Last Post: Zhavi221
  Using a lambda function within another function JChapman 8 994 Jan-08-2019, 01:54 PM
Last Post: JChapman
  How can I get values inside of function? Mysth 4 493 Dec-31-2018, 03:52 PM
Last Post: Axel_Erfurt

Forum Jump:

Users browsing this thread: 1 Guest(s)