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;
}







0















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?










share|improve this 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


















0















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?










share|improve this 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














0












0








0


0






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















3















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.






share|improve this answer

































    2














    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.






    share|improve this answer
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3















      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.






      share|improve this answer






























        3















        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.






        share|improve this answer




























          3












          3








          3








          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.






          share|improve this answer
















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 8:48

























          answered Jan 3 at 8:34









          Matteo ItaliaMatteo Italia

          103k15149250




          103k15149250

























              2














              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.






              share|improve this answer






























                2














                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.






                share|improve this answer




























                  2












                  2








                  2







                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 8:56









                  Matteo Italia

                  103k15149250




                  103k15149250










                  answered Jan 3 at 8:51









                  Edmond SesayEdmond Sesay

                  574




                  574















                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith