assignments of function references - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: assignments of function references (/thread-20504.html) |
assignments of function references - Skaperen - Aug-14-2019 i want to have class attribute names which could otherwise be used as variables be used for function references so i can keep everything for a thread in the same class. since a "def" is like an assignment, too, can i assign to a class attribute like this? class foo(): ... foo.var1 = 0 ... def foo.fun1(bar): foo.count += 1 return bar*bari no longer like to just try things to "see if they work" since i have found that modifying the locals() dictionary is an example of something that looks like it might work, but doesn't always do so. RE: assignments of function references - boring_accountant - Aug-14-2019 Can you clarify what you want to do ? I do not understand the following: Quote:i want to have class attribute names which could otherwise be used as variables be used for function referencesAs for your code, I don't think that the syntax you are using exists in Python. If you want to assign a function to a class attribute, you can do the following: def myfunc(): print('my_func') class foo: func = myfunc foo.func()
RE: assignments of function references - Skaperen - Aug-15-2019 can i do: def myfunc(): print('my_func') class foo: foo.func = myfunc foo.func()? RE: assignments of function references - fishhook - Aug-15-2019 (Aug-15-2019, 12:42 AM)Skaperen Wrote: can i do: yes, you can def myfunc(): print('my_func') class foo: pass foo.func = staticmethod(myfunc) foo.func()But I'm sure that you really don't need that |