Some Python codes display "__class__" when creating certain classes. What is it for?

Asked

Viewed 193 times

3

I realized that in certain codes in Python is used a __class__. What good is the same?

Example:

class abstract1 (object):
  def __init__(self):
    if self.__class__ == abstract1: 
      raise NotImplementedError("Interfaces can't be instantiated")
  • Could you give an example of where you saw this?

  • 1

    James, __class__ and __init__ are completely different things and I confess that I’ve never seen anyone manipulate __class__ of a class. You could [Dit] your question and explain better what you saw?

  • 3

    Taking advantage of that edited, we will try to make more clear. The __class__ which quote is from which language? In the title you say "some languages" and in the body of the question you present an example in Python. The question is about Python only? We can assume now that you know what the method is __init__?

1 answer

3

All objects in Python have some special fields that are read-only and store information about the same that is used by the language and can be used by the developer.

The field __class__ is one of them and stores a reference to the class of that object. That is, when you have an instance of a class you can access which class was instantiated.

A simple example:

class Foo:
    ...

obj = Foo()

print(obj.__class__ is Foo)  # True

About the behavior of is, read In Python, what are the consequences of using is instead of '=='

A curiosity is that the class itself is an instance and has a class:

print(Foo.__class__)  # <class 'type'>

Now your case:

class abstract1 (object):
  def __init__(self):
    if self.__class__ == abstract1: 
      raise NotImplementedError("Interfaces can't be instantiated")

You set a class with a name abstract1; she inherits from the class object (this was required in Python 2, but not in Python 3); it has an initializer method __init__ which will be called when an instance of this class is created. As the goal would be to abstract the abstract class concept, which cannot be instantiated, within the initializer method it is verified whether the class used in the instance, self.__class__, is the class itself abstract1, that cannot be instantiated. If so, it raises an exception NotImplementedError.

So when you try to create an instance of this class, obj = abstract1(), you will have an exit similar to:

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    obj = abstract1()
  File "main.py", line 4, in __init__
    raise NotImplementedError("Interfaces can't be instantiated")
NotImplementedError: Interfaces can't be instantiated

But if you inherit the class you will be able to instantiate normally:

class Filha(abstract1):
    ...

obj = Filha()

So self.__class__ will be Filha and no more abstract1.

For more information, Python has a native module for working with abstract classes, Abstract Base Classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.