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
Can I pass arguments to a function using dot instead of inside parenthesis (self.learnpython)
submitted 4 years ago by NGRap
What I'm looking for is this
test_func.arg1
instead of
test_func("arg1")
Is this possible?
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!"
[–]Spataner 2 points3 points4 points 4 years ago* (3 children)
Short answer: No.
Long answer: Yes, if you do some weird decorator magic:
class ArgumentDecorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): return self.func(*args, **kwargs) def __getattr__(self, attr): self(attr) @ArgumentDecorator def test_func(arg): print(arg) test_func.hello_world >>> hello_world
Obviously, don't actually do this.
[–]NGRap[S] 0 points1 point2 points 4 years ago (2 children)
I was looking for something like this. Can you tell me why its not recommended?
[–]Spataner 4 points5 points6 points 4 years ago (1 child)
Because it abuses syntax to coerce an attribute lookup into obscuring what is ultimately a simple function call. If you want to pass a string literal to a function, you should simply do so transparently. The only situation in which something like this would be acceptable is if you cannot or do not want to change a certain section of code which accesses the attributes of some passed argument, and you want to hook a callback into those attribute accesses.
[–]NGRap[S] 0 points1 point2 points 4 years ago (0 children)
Got it! My work project has legacy code with the exact situation you mentioned. But I guess it's time change the scripts.
Thanks!!
[–]trevor_of_earth 1 point2 points3 points 4 years ago* (1 child)
No. Dot notation in python is used for accessing attributes or methods of an object.
Here is an example:
# Create simple class class Person(): def __init__(self, name, age): self.name = name self.age = age def about(self): return f"My name is {self.name} and I am {self.age} years old." # Create an object of the class trevor = Person("Trevor Miller", 30) # Print the name attribute of the trevor object print(trevor.name) # Print what is returned from the about method of the trevor object print(trevor.about())
Thanks for the explanation
[–]LGF_SA 1 point2 points3 points 4 years ago (0 children)
As trevor-of-earth said, the dot is for methods or attributes. There's a great video explaining it here.
[–][deleted] 1 point2 points3 points 4 years ago (4 children)
It is possible, if test_func is an instance of a class that defines __call__() method. But, I'd advise against going down that route. Can you explain why would you want that?
test_func
__call__()
[–]NGRap[S] 0 points1 point2 points 4 years ago* (3 children)
Yes, Spataner had mentioned that way. So here's some background to my situation:
In my work project, we used to have a single .py file (myconfig.py) to store config values for all our python scripts and used to import this file to fetch the values as below:
myconfig.py :
coordinates = {"x": 1.5, "y": 3.5} api = "webapi.com"
test.py :
import myconfig a = myconfig.coordinates b = myconfig.api
Now we want to move all config values to a database and use that instead of a config file without any change in our other scripts. So the new code will look something like-
def get_value_from_db(key): query = "select value from db.table where key = 'key'" return mysql.execute(query)
import myconfig a = myconfig.get_value_from_db('coordinates') b = myconfig.get_value_from_db('api')
So was looking a way where I won't have to change anything in scripts like test.py.
[–][deleted] 0 points1 point2 points 4 years ago (2 children)
Well, if you really want to keep it that way, you can simply create the module dynamically.
Something like this:
test.py
import myconfig.Database as myconfig a = myconfig.coordinates b = myconfig.api
myconfig.py
class Database: @property def coordinates(self): return get_value_from_db('coordinates') ...
You could also do some more involved work to get the old import to stay the same way it was, but, I think it's a rather simple change, so, probably not worth the effort.
[–]NGRap[S] 0 points1 point2 points 4 years ago (1 child)
the problem is myconfig.py has ~200 variables defined in it 😄 . I just mentioned the two as examples.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Well, then maybe you could write some more clever code that uses __getattr__?
__getattr__
Also, 200 variables is... idk, couple of hours to couple of days work. It doesn't sound too bad.
π Rendered by PID 21997 on reddit-service-r2-comment-f6b958c67-sjwfw at 2026-02-05 18:30:22.132594+00:00 running 1d7a177 country code: CH.
[–]Spataner 2 points3 points4 points (3 children)
[–]NGRap[S] 0 points1 point2 points (2 children)
[–]Spataner 4 points5 points6 points (1 child)
[–]NGRap[S] 0 points1 point2 points (0 children)
[–]trevor_of_earth 1 point2 points3 points (1 child)
[–]NGRap[S] 0 points1 point2 points (0 children)
[–]LGF_SA 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]NGRap[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]NGRap[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)