Why can methods in Python standard library be called as functions?

  Kiến thức lập trình

I am a beginner in Python and found a strange phenomenon that some methods in Python standard library can be called as functions.
For example, there is a method randint() defined in Class Random in module random.py.
As I understand, when we wanted to call it, we should import the module random first, then declare a instance of class Random so that method randint() can be called such as

import random as rd
aaa = rd.Random()
aaa.randint()

But I just find that randint() can be called as a function and it is unnecessary to do as above:

import random as rd
rd.randint()

So I wonder how does this illogical phenomenon happen?

Why can methods in Python standard library be called as functions?

New contributor

Yongji Wen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT