Why does smtplib instance not return a dictionary?

Asked

Viewed 34 times

0

I’m starting my studies in Python and I have a question. While reading the ebook Automate the Boring Stuff I come across an instance that returns a certain object.

import smtplib
smtpObj = smtplib.SMTP('smtp.example.com', 587)
type(smtpObj)

Will return:

<class 'smtplib.SMTP'>

That’s where my doubt comes in. Why the function type(), does not return?

<class 'dict'>

I came from Javascript, and I don’t know if there’s a difference between object and dictionary.

  • Yes, there is a difference. A dictionary is just an object type. In this case, the library smtplib implements the class SMTP and you’re defining an instance of it, just like the JS classes.

  • I’m not finding any material that detail this. I appreciate the clarification.

  • Have you checked the official documentation package?

  • In Python a "dictionary" is not the same thing as an "object" - they are very different things - perhaps the biggest difference from Python to javascript. I recommend making the official language tutorial, step by step, in https://docs.python.org/3/tutorial/index.html

1 answer

0


The Library smtplib (protocol client SMTP)

The module smtplib sets a client session object SMTP which can be used to send messages to any Internet machine with a listener daemon SMTP or ESMTP. For details about the operation SMTP and ESMTP, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (Service Extensions SMTP).

class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)

Dictionary in python

Dictionary is a different kind of collection. It is a type of mapping native to the Python. A map is a disorderly associative collection. The association, or mapping, is made from a key, which can be any immutable type, to a value, which can be any data object of the Python.

So, knowing this we can see clearly that there is a difference between a dictionary and the library smtplib... The library smtplib implements only the class SMTP (where you instance the same) and a dictionary is just a type of object, so it happens what you said in your question about the type(smtpObj).

NOTE

In this example of Alura you can clearly see the workings of a dictionary.

Browser other questions tagged

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