Answer the question
In order to leave comments, you need to log in
Do Python and Go have delegates?
I googled on the Internet and got the impression that there are no delegates in Python and Go. Rare mentions of their presence on the campaign are some kind of self-written bicycles.
Is it so? If so, why weren't they added to the languages? Do they complicate the language? What's in their place?
Answer the question
In order to leave comments, you need to log in
Delegation is a design pattern and can be implemented in most modern programming languages. There is an example for Python on the wiki , no additional keywords are required to implement delegates in python, because any method can be passed as a parameter anywhere. You can also put methods in data structures, here is an example:
def say_hello(name):
print 'Hello,', name
def say_goodbye(name):
print 'Bye,', name
phrases = {
'hello': say_hello,
'bye': say_goodbye,
}
def say(what, name):
phrases[what](name)
say('hello', 'Ivan')
say('bye', 'Oleg')
In Python, functions are first class citizens, they can be passed as arguments to other functions and stored in tuple array dictionaries. Therefore, there is simply no need for such a concept as delegates.
If you use C# terminology, then you would put a tag, you would answer more.
If not, skip this answer.
In most languages, the ability to work with functions as values is called first-class function values or first-class function objects, and the ability to bind some data to a function (more precisely, to some internal function variables that are not its parameters) is called a closure ( closure). Sharp delegates are actually the same function object, they are now more often called that. There is a really important difference regarding multicast delegates - in many languages this is not possible, but this is perhaps the only important difference, and in general it is not difficult to level it. In general, functional objects and closures are well supported in Python, including binding a method to an object using a dot (obj.method), for example:
>>> class A:
... def print_me(self):
... print(self.v)
...
>>> a = A()
>>> a.v = 91
>>> a.print_me()
91
>>> f = a.print_me
>>> a.v = 155
>>> f()
155
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question