#๐Ÿ”’ help with code

67 messages ยท Page 1 of 1 (latest)

topaz elk
scarlet fossilBOT
#

@topaz elk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

topaz elk
#

so in the line 55applyFunctionToFolders(top_level_folders, removeParentFolderPrefix)
i want to pass function parameters for the removeParentFolderPrefix()

#

because as you u can see in line 44 , it takes a prefix parameter

#

i dont know how exactly to proceed

#

or what my thought process should be in problems like this

unborn timber
#

you want partial application

topaz elk
#

whats that?

unborn timber
#

basically, creating a new function that would accept less arguments, forward them to call the original function and send some additional parameters
e.g.

f(x, y) = z
g(x) = f(x, 0)

that might be too abstract of an example but i hope its understandable

topaz elk
#

i do not understand how u can put ( , ) on the left hand side

unborn timber
#

it is pseudocode, more like math

#

!e

def f(x, y):
  return x + y
print((lambda x: f(x, 10))(2))
scarlet fossilBOT
topaz elk
#

will need to learn how lambda works

unborn timber
#

it is not required, you can use def just fine here too

topaz elk
#

lambda is like a function blueprint?

unborn timber
#

no, it is like def but: anonymous, is an expression, and limited to evaluating a single expression (so no multiple statements)

topaz elk
#

i think i am getting the gist of it

#

partial application

unborn timber
#

basically, you'd make a function thats like removeParentFolderPrefix, but only takes the path, and the prefix is already known and it would just call the original removeParentFolderPrefix with that path and prefix

#

though in applyFunctionToFolders you dont actually pass the folder path to the callable? and you use generic types without parametrizing them, e.g. just list. list of what? you can specify e.g. list[Path], same with just Callable: callable with what parameters and what does it return? Callable[[int, str], float] would be something like def f(x: int, y: str) -> float:

topaz elk
#

good idea! i will make it list[Path] since thats more suitable

#

so the reason i didnt make Callable more specific is , if i want to use a different function?

#

its a good idea right?

unborn timber
#

but the signature stays the same

topaz elk
#

does it?

unborn timber
#

yes, you will always be passing a folder path to the callable in the applyFunctionToFolders

topaz elk
#

right!

#

thanks

#

i still dont understand the initial part tho , how lambda works

unborn timber
#

lambda arguments: expression is the same as f in

def f(arguments):
  return expression

it is just a way to define a function in an expression, it is never required and can always be replaced with a def

topaz elk
#

so

def add(first , second):
    return first+second
lambda first, second : first+second
#

like that?

unborn timber
#

yes, those functions are equivalent, though the lambda version doesnt bind it to any name

topaz elk
#

๐Ÿ˜ฎ very interesting

#

why would u use lambda instead of a def?

unborn timber
#

to not have multiple statements, it is basically a non-reason, just something you can do. it is mostly used like in your case where you want to pass a function to another function, and you can express its body with just one expression so you can use it instead of def'ing a function, having to think of a name, ...

topaz elk
#

i see!

#

so

def mainFunction(x,y)
 return x+y
def additionalFunc(x):
   mainFunction(x,10)
#and i use it like this?
ParentFunction(additionalFunc(100))
#

wait what

unborn timber
#

you didnt make it return, and in this case it wasnt particularly useful - the reason for the partial application was that you wanted to pass a function but with the prefix argument set, not apply it fully

topaz elk
#

right! that doesnt look useful

#

how do u pass and not apply it?

unborn timber
#

ParentFunction(additionalFunc)

topaz elk
#

but then how would i pass arguments?

#

can just add *args...?

unborn timber
topaz elk
#
def greet(name: str, greeting: str) -> str:
    return f"{greeting}, {name}!"

def process_greeting(func, *args):
    # Call the passed function with the provided arguments
    return func(*args)

result = process_greeting(greet, "Alice", "Hello")

print(result)  # Output: "Hello, Alice!"
unborn timber
#

remember your original problem

#

you have apply_to_folders(top_level_folders, function_that_takes_a_folder_path)
you want to provide removeParentFolderPrefix as the function_that_takes_a_folder_path, but it takes 2 arguments: the path and a prefix, so you create another function ,that takes just the folder path, and forwards it to original function with some already known prefix

topaz elk
#

right and i want to somehow pass argument to the function_that_takes_a_folder_path

#

i dont understand the last part

unborn timber
#

!e

def f(xs, g):
  return [g(x) for x in xs]
def g(x, y):
  return x + y
print(f([1, 2, 3], lambda x: g(x, 5)))
scarlet fossilBOT
unborn timber
#

f(xs, g) expects g to have 1 argument, but in our case it had 2, and we wanted to call it with y = 5 each time, so we made a function that takes just 1 argument, and calls g with that argument and a constant 5 as the y argument

topaz elk
#

thats three functions?

unborn timber
#

yes

topaz elk
#

i also have 3 functions, applyfunction(), removePrefix(), and one that i need to make

unborn timber
#

yes, you'd make a function that takes just the folder path, and calls removeprefix with that folder path and some predefined prefix
(and make applyfunction actually call it with the folder path, currently you're not providing arguments to it, just doing folderOperation())

topaz elk
#

thank you for explaining this to me! i will try to make it now

#

wait

#

applyFunctionToFolders(top_level_folders, lambda x : removeFolderPrefix(x,"my Prefix"))

#

like this?

unborn timber
#

yep

topaz elk
#

thank you!

scarlet fossilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.