Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dynamic dispatch and duck typing?

When using Pycharm, It often points out an error, saying:

Unresolved reference 'name'. This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

I've snooped around about this, but most questions and information I find is about preventing the message from being shown. what I want to know is:

  • What is dynamic dispatch/duck typing?
  • What are (or an example of) these "useful number of cases"?
like image 612
Rick M. Avatar asked Feb 14 '18 21:02

Rick M.


People also ask

What is duck typing?

Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing, you do not check types at all. Instead, you check for the presence of a given method or attribute.

Is duck typing good?

advantage is that it leads to fewer lines of code. This makes it look cleaner; thus making your code easier to read and faster to write. practices, it really is useful functionality!

Where is duck typing used?

Duck Typing is a type system used in dynamic languages. For example, Python, Perl, Ruby, PHP, Javascript, etc. where the type or the class of an object is less important than the method it defines. Using Duck Typing, we do not check types at all.

Why is duck typing used in Python?

The main reason for using duck typing is to provide support for dynamic typing in Python programming. In Python, we don't need to specify the variable's data type and we can reassign the different data type values to same variable in further code. Let's see the following example.


2 Answers

Python uses a duck typing convention. This means that you do not have to specify what type a name is. Unlike in Java, for example, where you must specify explicitly that variable may be type int or Object. Essentially, type checking is done at runtime.

"If it walks like a duck and it quacks like a duck, then it must be a duck."

In Python everything will seem to work until you use try to manipulate an object in a way that it is not designed to. Basically, an object may not have a certain method or attribute that another might, and you won't find this out until Python throws an error upon trying it.

Dynamic Dispatch is the practice of the compiler or environment choosing which version of a polymorphic function to use at runtime. If you have multiple implementations of a method, you can use them in different ways despite the methods having the same or similar properties/attributes. Here's an example:

class Foo:
   def flush():
       pass

class Bar:
    def flush():
       pass

Both classes have a flush() method but the correct name is chosen at runtime.

Python is not the best example of this process since methods can take multiple parameters and don't have to be reimplemented. Java is a better example, but I'm not fluent enough in it to provide a correct example.

like image 70
smallpants Avatar answered Sep 23 '22 01:09

smallpants


The warning means that you're using a variable that PyCharm doesn't recognise, but due to Python's dynamic nature it can't be sure if it's right or you're right.

For example you may have the following code:

class myClass():

    def myfunc(self):
        print(self.name)

PyCharm will probably complain that self.name can't be resolved. However, you may use the class like this:

my_class = myClass()
my_class.name = "Alastair"
my_class.myfunc()

which is perfectly valid (albeit brittle).

The message goes on to say that it's more confident about attribute and methods that are less ambiguous. For example:

class myClass():
    my_instance_var = "Al"

    def myfunc(self):
        print(self.my_instance_var)

As my_instance_var is defined in the source code (a class attribute), PyCharm can be confident it exists.

(Don't use class attributes unless you know what you're doing!)

like image 32
Alastair McCormack Avatar answered Sep 23 '22 01:09

Alastair McCormack