What does '__main__' mean in the output of type() [closed]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
If I create a class as below, and check the type of the object, I get the following output.
My question is what does __main__
mean here?
class Student(object):
pass
>>>a = Student()
>>>type(a)
<class '__main__.Student'>
There is another question, if I check the type of the Student class, I get the following output.
>>>type(Student)
<class 'type'>
What does <class 'type'>
mean here?
python python-3.x python-2.7
closed as off-topic by iBug, jpp, grizzthedj, Bob Dalgleish, thewaywewere Jan 3 at 16:39
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
If I create a class as below, and check the type of the object, I get the following output.
My question is what does __main__
mean here?
class Student(object):
pass
>>>a = Student()
>>>type(a)
<class '__main__.Student'>
There is another question, if I check the type of the Student class, I get the following output.
>>>type(Student)
<class 'type'>
What does <class 'type'>
mean here?
python python-3.x python-2.7
closed as off-topic by iBug, jpp, grizzthedj, Bob Dalgleish, thewaywewere Jan 3 at 16:39
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
4
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
3
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38
add a comment |
If I create a class as below, and check the type of the object, I get the following output.
My question is what does __main__
mean here?
class Student(object):
pass
>>>a = Student()
>>>type(a)
<class '__main__.Student'>
There is another question, if I check the type of the Student class, I get the following output.
>>>type(Student)
<class 'type'>
What does <class 'type'>
mean here?
python python-3.x python-2.7
If I create a class as below, and check the type of the object, I get the following output.
My question is what does __main__
mean here?
class Student(object):
pass
>>>a = Student()
>>>type(a)
<class '__main__.Student'>
There is another question, if I check the type of the Student class, I get the following output.
>>>type(Student)
<class 'type'>
What does <class 'type'>
mean here?
python python-3.x python-2.7
python python-3.x python-2.7
edited Jan 3 at 8:36


finefoot
2,81641937
2,81641937
asked Jan 3 at 8:25
AndyAndy
102
102
closed as off-topic by iBug, jpp, grizzthedj, Bob Dalgleish, thewaywewere Jan 3 at 16:39
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by iBug, jpp, grizzthedj, Bob Dalgleish, thewaywewere Jan 3 at 16:39
- This question does not appear to be about programming within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.
4
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
3
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38
add a comment |
4
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
3
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38
4
4
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
3
3
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38
add a comment |
2 Answers
2
active
oldest
votes
My question is what does
'__main__'
mean here?
__main__
there is the module in which Student
is defined; the module corresponding to the file that you start with the Python interpreter is automatically named __main__
. You may remember it from the usual idiom
if __name__ == '__main__':
...
that checks if the name of the current module is __main__
to see if this is the script that has been run (as opposed to it being imported as a module).
If you defined Student
inside another file, and imported it from your main module, it would have said the name of such module instead. For example:
run.py
import student
class Student(object):
pass
a = student.Student()
print(type(a))
b = Student()
print(type(b))
student.py
class Student(object):
pass
if you run python run.py
you'll get
<class 'student.Student'>
<class '__main__.Student'>
where you'll see confirmation that the name before the dot is indeed the module where the given type is defined (useful, as in this case, to disambiguate and to get at a glance where some given type is defined).
What does
<class 'type'>
mean here?
It means that the Student
class, as all classes defined with class
, is in turn an instance of the builtin type type
. It may get a little circular, but classes themselves are instances of metaclasses; for all the gory detail about how this works under the hood, you may have a look at this question, but it isn't light reading.
add a comment |
The __main__
in '__main__.Student'
is saying that the Student object (or class) was defined in the scope in which the top level code is being executed (the __main__
scope). If the Student
class was defined in another module, call it imported_module
, and imported into the main scope, then the print(type(a))
would output imported_module.Student
. So basically, the type of an object always refers back to the scope in which it was defined.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
My question is what does
'__main__'
mean here?
__main__
there is the module in which Student
is defined; the module corresponding to the file that you start with the Python interpreter is automatically named __main__
. You may remember it from the usual idiom
if __name__ == '__main__':
...
that checks if the name of the current module is __main__
to see if this is the script that has been run (as opposed to it being imported as a module).
If you defined Student
inside another file, and imported it from your main module, it would have said the name of such module instead. For example:
run.py
import student
class Student(object):
pass
a = student.Student()
print(type(a))
b = Student()
print(type(b))
student.py
class Student(object):
pass
if you run python run.py
you'll get
<class 'student.Student'>
<class '__main__.Student'>
where you'll see confirmation that the name before the dot is indeed the module where the given type is defined (useful, as in this case, to disambiguate and to get at a glance where some given type is defined).
What does
<class 'type'>
mean here?
It means that the Student
class, as all classes defined with class
, is in turn an instance of the builtin type type
. It may get a little circular, but classes themselves are instances of metaclasses; for all the gory detail about how this works under the hood, you may have a look at this question, but it isn't light reading.
add a comment |
My question is what does
'__main__'
mean here?
__main__
there is the module in which Student
is defined; the module corresponding to the file that you start with the Python interpreter is automatically named __main__
. You may remember it from the usual idiom
if __name__ == '__main__':
...
that checks if the name of the current module is __main__
to see if this is the script that has been run (as opposed to it being imported as a module).
If you defined Student
inside another file, and imported it from your main module, it would have said the name of such module instead. For example:
run.py
import student
class Student(object):
pass
a = student.Student()
print(type(a))
b = Student()
print(type(b))
student.py
class Student(object):
pass
if you run python run.py
you'll get
<class 'student.Student'>
<class '__main__.Student'>
where you'll see confirmation that the name before the dot is indeed the module where the given type is defined (useful, as in this case, to disambiguate and to get at a glance where some given type is defined).
What does
<class 'type'>
mean here?
It means that the Student
class, as all classes defined with class
, is in turn an instance of the builtin type type
. It may get a little circular, but classes themselves are instances of metaclasses; for all the gory detail about how this works under the hood, you may have a look at this question, but it isn't light reading.
add a comment |
My question is what does
'__main__'
mean here?
__main__
there is the module in which Student
is defined; the module corresponding to the file that you start with the Python interpreter is automatically named __main__
. You may remember it from the usual idiom
if __name__ == '__main__':
...
that checks if the name of the current module is __main__
to see if this is the script that has been run (as opposed to it being imported as a module).
If you defined Student
inside another file, and imported it from your main module, it would have said the name of such module instead. For example:
run.py
import student
class Student(object):
pass
a = student.Student()
print(type(a))
b = Student()
print(type(b))
student.py
class Student(object):
pass
if you run python run.py
you'll get
<class 'student.Student'>
<class '__main__.Student'>
where you'll see confirmation that the name before the dot is indeed the module where the given type is defined (useful, as in this case, to disambiguate and to get at a glance where some given type is defined).
What does
<class 'type'>
mean here?
It means that the Student
class, as all classes defined with class
, is in turn an instance of the builtin type type
. It may get a little circular, but classes themselves are instances of metaclasses; for all the gory detail about how this works under the hood, you may have a look at this question, but it isn't light reading.
My question is what does
'__main__'
mean here?
__main__
there is the module in which Student
is defined; the module corresponding to the file that you start with the Python interpreter is automatically named __main__
. You may remember it from the usual idiom
if __name__ == '__main__':
...
that checks if the name of the current module is __main__
to see if this is the script that has been run (as opposed to it being imported as a module).
If you defined Student
inside another file, and imported it from your main module, it would have said the name of such module instead. For example:
run.py
import student
class Student(object):
pass
a = student.Student()
print(type(a))
b = Student()
print(type(b))
student.py
class Student(object):
pass
if you run python run.py
you'll get
<class 'student.Student'>
<class '__main__.Student'>
where you'll see confirmation that the name before the dot is indeed the module where the given type is defined (useful, as in this case, to disambiguate and to get at a glance where some given type is defined).
What does
<class 'type'>
mean here?
It means that the Student
class, as all classes defined with class
, is in turn an instance of the builtin type type
. It may get a little circular, but classes themselves are instances of metaclasses; for all the gory detail about how this works under the hood, you may have a look at this question, but it isn't light reading.
edited Jan 3 at 8:48
answered Jan 3 at 8:34
Matteo ItaliaMatteo Italia
103k15149250
103k15149250
add a comment |
add a comment |
The __main__
in '__main__.Student'
is saying that the Student object (or class) was defined in the scope in which the top level code is being executed (the __main__
scope). If the Student
class was defined in another module, call it imported_module
, and imported into the main scope, then the print(type(a))
would output imported_module.Student
. So basically, the type of an object always refers back to the scope in which it was defined.
add a comment |
The __main__
in '__main__.Student'
is saying that the Student object (or class) was defined in the scope in which the top level code is being executed (the __main__
scope). If the Student
class was defined in another module, call it imported_module
, and imported into the main scope, then the print(type(a))
would output imported_module.Student
. So basically, the type of an object always refers back to the scope in which it was defined.
add a comment |
The __main__
in '__main__.Student'
is saying that the Student object (or class) was defined in the scope in which the top level code is being executed (the __main__
scope). If the Student
class was defined in another module, call it imported_module
, and imported into the main scope, then the print(type(a))
would output imported_module.Student
. So basically, the type of an object always refers back to the scope in which it was defined.
The __main__
in '__main__.Student'
is saying that the Student object (or class) was defined in the scope in which the top level code is being executed (the __main__
scope). If the Student
class was defined in another module, call it imported_module
, and imported into the main scope, then the print(type(a))
would output imported_module.Student
. So basically, the type of an object always refers back to the scope in which it was defined.
edited Jan 3 at 8:56
Matteo Italia
103k15149250
103k15149250
answered Jan 3 at 8:51


Edmond SesayEdmond Sesay
574
574
add a comment |
add a comment |
4
Did you try googling and reading documentation before posting here?
– DYZ
Jan 3 at 8:32
3
Possible duplicate of What does if __name__ == "__main__": do?
– Johnny
Jan 3 at 8:32
I read that before. I have two questions. Please read it carefully. In addition, I'm not quite sure if _main_ here is same as the one in the doc.
– Andy
Jan 3 at 8:38