#๐ More pythonic way of handling output of function that can return 1 or 2 arguments?
57 messages ยท Page 1 of 1 (latest)
@lone fern
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.
Why does the function return different shapes sometimes?
because foo() can be multiple different functions
what do you mean?
def outer(foo: Callable)
maybe2 = foo() # foo can return 1 or 2 arguments
try:
a, b = maybe2
except ValueError:
a = maybe2
try:
foo2(a, b)
except (SomeError, NameError):
foo3(a)
outer(someFunc)
outer(someFunc2)
Do you have some examples of functions you'd pass here?
What kind of objects do they return?
a is a discord embed
b is a filepath string
it's a path to an icon for the embed
foo is a function that creates an embed that may or may not have a thumbnail
Sounds like you want to always return embed, filepath. If there's no thumbnail, return None as the second element of the tuple
def outer(foo):
embed, filepath = foo()
if filepath is None:
return foo3(embed)
else:
return foo2(embed, filepath)
you forgot about the SomeError
if foo2 returns SomeError I want the same behavior as if there was no b
def outer(foo):
embed, filepath = foo()
if filepath is None:
return foo3(embed)
else:
try:
return foo2(embed, filepath)
except SomeError:
return foo3(embed)
what is SomeError?
FileNotFound
Maybe that try-except belongs inside foo2?
If the file is missing, log some warning message and return an embed as if there was no thumbnail
actually I'm not sure why you'd need this outer function
If you know you have an embed with thumbnail, call foo2. If you know that you don't have a thumbnail, call foo3
Maybe you could have a single foo1 function that accepts an optional thumbnail_path parameter
if you really want more context
i can't put the try except inside foo2
since that is interaction.response.edit_message
what are outer, foo, foo2 and foo3 here?
Wait. you are always returning embed, fullIconPath from match_and_create_embed
there's another function besides match_and_create_embed
that will not be returning fullIconPath
return embed, None from it then
If you're parameterizing something with a function, it's better if it has a uniform interface. It is a big pain to figure out what the function intended to return if it's very diverse
If the icon file doesn't exist, do you really want it to pass silently?
it's not silent
i have a print statement
that specifies when it gets returned without an icon
oh you mean separately from intentionally returning without icon?
i mean i'll see it
and it's expected in most casese
I'd do something like this:
embed, fullIconPath = embedAndIconPath
if fullIconPath is None:
await interaction.response.edit_message(content=None, embed=embed, view=None)
print('Successfully returned request without icon after editing.')
return
image = discord.File(fullIconPath, filename='icon.png')
embed.set_thumbnail(url="attachment://icon.png")
try:
await interaction.response.edit_message(content=None, embed=embed, attachments=[image], view=None)
print('Successfully returned request after editing.')
except FileNotFoundError:
await interaction.response.edit_message(content=None, embed=embed, view=None)
print(f'Icon {fullIconPath!r} was not found. Sending embed without icon')
Catching an error for an undefined variable is a pretty non-obvious move (what variable are you catching?), and it's definitely not the idiomatic way.
pattern matching would be cleaner
that is also possible
what's that
Also, NameError is the kind of error that mostly indicates a programming mistake, and you should avoid catching them. You might have a typo in a variable name (like photo instead of image), and you'll have a hard time debugging that
this seems like a very neat feature
im trying to build a query command for my bot with stuff like AND and NOT and OR
could be useful for that
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.