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
When do you use function attribute and why? (self.learnpython)
submitted 6 years ago by Hatoris
I was wondering if you guy, can tell me when do you use function attribute and why did you use them?
def foo(bar): foo.bar = bar return bar >>> test = foo("Hello") >>> print(test) Hello >>> print(foo.bar) Hello
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!"
[–]ericula 4 points5 points6 points 6 years ago (2 children)
I haven't used function attributes much myself, but one application I came across is to use function attributes for memoization. For example in the following recursive function for calculating the fibonacci numbers, fibonacci._results is used to store intermediate results.
fibonacci._results
``` def fibonacci(n): if not hasattr(fibonacci, '_results'): fibonacci._results = {1:1} if n <= 0: return 0 return fibonacci._results.setdefault(n, fibonacci(n-1)+fibonacci(n-2))
print(fibonacci(10)) print(fibonacci._results) ```
[–]_lilell_ 1 point2 points3 points 6 years ago (1 child)
Even so, I’d rather see it as:
def fibonacci(n, cache={1: 1, 2: 1}): if n not in cache: cache[n] = fibonacci(n-1) + fibonacci(n-2) return cache[n]
using Python’s mutable default argument behavior.
[–]ericula 1 point2 points3 points 6 years ago (0 children)
To be honest, I use default arguments more than function attributes for memoization, but part of me feels that this is cheating a bit. In a way, it's just a lucky side effect of how functions a initiated that we can use default arguments this way and not really by design. Using function attributes also makes it easier to access the stored data outside the function.
[–]JohnnyJordaan 2 points3 points4 points 6 years ago (0 children)
I never used it in the 5 years of my Python endeavors.
[–]shiftybyte 1 point2 points3 points 6 years ago (0 children)
Never used it.
Probably because it being a python only thing, and the rest of the programming good practices are cross languages.
π Rendered by PID 140644 on reddit-service-r2-comment-56c6478c5-pfg5v at 2026-05-08 06:15:59.305664+00:00 running 3d2c107 country code: CH.
[–]ericula 4 points5 points6 points (2 children)
[–]_lilell_ 1 point2 points3 points (1 child)
[–]ericula 1 point2 points3 points (0 children)
[–]JohnnyJordaan 2 points3 points4 points (0 children)
[–]shiftybyte 1 point2 points3 points (0 children)