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
#1
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.
Reply
#2
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.
Reply
#3
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):
    gD.fill(black)
    health = getattr(p, 'damageTaken')
    healthSub = health - (health * 2)
    p.damageTake(healthSub)
    pygame.display.update()
    time.sleep(5)

Cabin = Building(260, 30, 200, 200, Buildings[1], openCabin(p))
The function executes one line 9 where it says "openCabin"
Reply
#4
https://docs.python.org/3/library/functo...ls.partial 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))
Reply
#5
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
Use:
>>> 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)
42
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  not able to call the variable inside the if/elif function mareeswaran 3 501 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  The function of double underscore back and front in a class function name? Pedroski55 9 2,367 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 1,237 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  with open context inside of a recursive function billykid999 1 1,269 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 1,845 May-02-2023, 08:40 AM
Last Post: Gribouillis
Question How to compare two parameters in a function that has *args? Milan 4 2,703 Mar-26-2023, 07:43 PM
Last Post: Milan
  function accepts infinite parameters and returns a graph with those values edencthompson 0 1,319 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Exit function from nested function based on user input Turtle 5 4,263 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 4,632 May-02-2021, 12:21 PM
Last Post: Gribouillis
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,941 Mar-04-2021, 10:47 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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