Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 assignments of function references
#1
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*bar
i 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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
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 references
As 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()
Output:
my_func
Quote
#3
can i do:

def myfunc():
    print('my_func')

class foo:

foo.func = myfunc

foo.func()
?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
(Aug-15-2019, 12:42 AM)Skaperen Wrote: can i do:

def myfunc():
    print('my_func')

class foo:

foo.func = myfunc

foo.func()
?


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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to print the names of assignments if they are randomly selected Kongurinn 2 968 Oct-22-2017, 07:31 AM
Last Post: buran
  module functions and data references Skaperen 6 1,479 Jul-30-2017, 04:09 AM
Last Post: Skaperen
  Looking for Python Assignments BillGates 3 1,395 Mar-06-2017, 04:31 PM
Last Post: BillGates

Forum Jump:


Users browsing this thread: 1 Guest(s)