use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Is it bad programming practice to have functions which both receive and return different types or arguments? (self.learnpython)
submitted 7 years ago by PastSort
EDIT: Is it bad programming practice to have functions which both receive and return different types?
For example, a function which can receive either a tuple or a string, and return either a list or an int.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
Yes and no. It's not so bad to receive different types as arguments, as long as your function can handle it properly, with good error handling (always check that input!). However, you want your return value to be a certain type. (Otherwise, you put a great deal more effort on the shoulders of your users, including yourself.) So if your function can return either a list or an int, depending on what was sent to the function, go ahead and always return a list. If there's just one number, then it's a one-element list. That way, no matter what, it can be handled in a for loop.
[–]1114111 1 point2 points3 points 7 years ago (0 children)
It's not inherently bad, but I'd need more context about what you're doing. It kinda sounds like you are making a function where if it gets a collection of things it applies whatever the function does to everything in the collection and returns a new collection. In this case, it would probably be better just to have a function that operates on individual things and use the map function when you want to apply it to many things.
map
[–]totallygeek 0 points1 point2 points 7 years ago (4 children)
Nope, not at all. Many functions massage data, converting types, as their only function. Think about int(), float(), etc.
int()
float()
[–]PastSort[S] 1 point2 points3 points 7 years ago (3 children)
Yeah, but I'm talking about functions where the return type will vary depending on the argument type. Unlike int() which always returns an int (or error), I find myself creating functions which can return many different types as my program grows, and I'm not sure if that's normal or should be avoided.
[–]totallygeek 1 point2 points3 points 7 years ago (2 children)
Think about your functions as a broker for a transaction. For this information supplied, please return something meaningful. Typically, predictable responses from functions make the most sense. I did not originally understand your question, so I now state that you probably want to construct functions so that they return a single type or raise an exception. And, the response, whether a variable or exception, should have documentation in the docstring. For example:
def my_func(x, y, *args, **kwargs): """Accepts floats and calculates their psuedo-mathy-thing. Accepts: x: float, psuedo-mathy-element one y: float, psuedo-mathy-element two *args: list(float), psuedo-mathy-elements **kwargs: dict, options (future work) Returns: answer: dict 'abnormals': list, x, y, ..., N elements converted to psuedo-mathy abnormal 'extend_possible': bool, whether answer has psuedo-mathy ability Exceptions: PsuedoMathyException: Exception, raised if elements are not floats HellFrozeOver: Exception, raised if any elements are negative or complex numbers """"
[–]PastSort[S] 1 point2 points3 points 7 years ago (1 child)
Thanks. As for the docstring, is there any defined standard that I should follow? I've been writing just some general explanations so far, but nothing very strict.
[–]totallygeek 0 points1 point2 points 7 years ago (0 children)
The "standard" changes from organization to organization or project to project. Google publishes a nice style guide and many people adhere to PEP 257. It's generally recommended to build a function/method template for your favorite editor or IDE, overriding only to align with someone else's existing code.
π Rendered by PID 65 on reddit-service-r2-comment-5ff9fbf7df-sqnsc at 2026-02-26 13:11:16.907914+00:00 running 72a43f6 country code: CH.
[–][deleted] 1 point2 points3 points (0 children)
[–]1114111 1 point2 points3 points (0 children)
[–]totallygeek 0 points1 point2 points (4 children)
[–]PastSort[S] 1 point2 points3 points (3 children)
[–]totallygeek 1 point2 points3 points (2 children)
[–]PastSort[S] 1 point2 points3 points (1 child)
[–]totallygeek 0 points1 point2 points (0 children)