#๐ help with code
67 messages ยท Page 1 of 1 (latest)
@topaz elk
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.
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
you want partial application
whats that?
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
i do not understand how u can put ( , ) on the left hand side
it is pseudocode, more like math
!e
def f(x, y):
return x + y
print((lambda x: f(x, 10))(2))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
12
will need to learn how lambda works
it is not required, you can use def just fine here too
lambda is like a function blueprint?
no, it is like def but: anonymous, is an expression, and limited to evaluating a single expression (so no multiple statements)
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:
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?
but the signature stays the same
does it?
yes, you will always be passing a folder path to the callable in the applyFunctionToFolders
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
so
def add(first , second):
return first+second
lambda first, second : first+second
like that?
yes, those functions are equivalent, though the lambda version doesnt bind it to any name
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, ...
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
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
ParentFunction(additionalFunc)
the thing we went over was doing just that, but maybe you need an example
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!"
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
right and i want to somehow pass argument to the function_that_takes_a_folder_path
i dont understand the last part
!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)))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[6, 7, 8]
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
thats three functions?
yes
i also have 3 functions, applyfunction(), removePrefix(), and one that i need to make
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())
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?
yep
thank you!
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.