logo
Send your CVContact

/

logo
Background image

Blog

Type() function - when class is not enough

Dawid Trendota 06.11.2018

Today, one of the built-in functions – type() function – goes to the workshop.It usually takes one argument and returns the type of argument passed to it.

class type(object)

This is the kind of character we learn during python courses, which we use when debugging. It is worth remembering its use quickly with the help of python intepreter:

>>> val = 10
>>> val2 = „Hello World”

>>> type(val)

<class 'int’=””>

>>> type(val2)

<class 'str’></class>

As you can see, there is no major surprise here. Have you ever wondered what type is returned? This can also be checked by the type() function.

>>> type(type(val))<class 'type’>

While the results so far have been quite obvious, now the feature has returned us a mysterious type of type.It’s a so-called metaclass. We will get a similar effect if we check the built-in types.

>>> type(int)

<class 'type’=””>

>>> type(float)

<class 'type’></class>

And also the classes created by us.

>>> Foo class:
…     Pass

>>> type(Foo())
<class '__main__.foo’=””>
>>> type(Foo)
<class 'type’></class>

What are metaclasses? Briefly, for this article, you only need to explain that metaclasses are what classes are for instances.

diagram1 type() function: http://yuml.me/diagram/scruffy/class/edit[type{bg:steelblue}]/^-[type]-,[type] ^-[class]-,[class{bg:wheat}] ^--[instance{bg:green}]
https://yuml.me – I reco

mmendYou both Foo and int or float are instances of metaclass type.

How do I not use type()?

Although every programmer writes the code to the best of our ability, each of us – sooner or later – will encounter this type of pasta nightmare. Of course, written by someone else who has long been no longer working in our organization :).

The type() function is used here to make some decisions based on the type of argument passed to it. But where does this nightmare actually lurk? You can rely on a comprehensive list of articles that accurately use this feature as an example of python antipattern. But we don’t have to look that far. Documentation of the function itself is undoubtedly the best argument.

[box type=”info”] With one argument, return the type of an object. The return value is a type object. The isinstance() built-in function is recommended for testing the type of an object.

https://docs.python.org/3.7/library/functions.html#type


Accor[/box]ding to the documentation, the use of the isinstance() function will be better here. With such comparisons, when we want the expression to be true for the base class, we almost always want to achieve the same with the derived class as well. The type() function unfortunately does not give us any support mechanisms for polymorphism, meanwhile isinstance() – as much as possible. You can see a comparison of these two features in the following example.

Is it good now? One rabbi will say yes and another will say no. In fact, there are not many cases where the control flow should depend on the type passed. Such written code certainly raises doubts.

The article could have ended here if it hadn’t been for the second form of the feature that few had really come into contact with.

Type() function – when class is not enough

Three parameters can also be passed to our function:

class type(name, bases, dict)


At the output, we will then receive a new instance of the type metaclass, that is, a new class! A class called name, base classes, and dict dictionary. So we can freely shape its methods or variables. This gives us opportunities dedicated to the class keyword, but can be used in runtime. But what does it look like in practice?

What actually happened here? In the runtime, we created a new Cat class based on the existing Animal, and the my_init was passed on __init__ new class. In addition, we have found that such a created class does not stand out functionally from its counterparts created with class. But can we imagine any practical application for this functionality? There is no doubt that we can play around with academic examples, but can there be a situation in practice where we do not know what classes we will need?

Yes, it happens. For example, we might want to present a database so that each table has its equivalent as a class. Python knows nothing about what classes will be needed until it opens the database. Due to the limitations repl.it – the following example is based on a „database” in json.

As you can see, we can list entire sets of class instances, even if we didn’t know about them as we typed.This use of type() allows for a very flexible approach to many problems, which should be remembered when working with external data sources.

And knowledge of this is undoubtedly useful for :).

logo
ContactSend CV