0

can some one please share their knowledge on instance vs object, i am using python 2.7 version.

  1. i created a class and assigned to a variable t

    class test():
        pass
    
    t=test()
    

    at this point i thought t is an object of class test but when i print i get a instance

    print t
    <__main__.test instance at 0x7fb1a3562b00>
    
    type(t)
    <type 'instance'>
    
  2. now i created another class with object

    class Spam(object): 
        pass
    
    s=Spam()
    

    so when i printed this i was expecting it to be an object or instance , but i get a class type.

    print s
    <__main__.Spam object at 0x7fb1a3560f10>
    >>> type(s)
    <class '__main__.Spam'>
    

i thought object is the base class from which all the classes are derived, if i mention in the class def or not. i am confused.

please share your thoughts

thanks pradeep

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
pradeep
  • 53
  • 5
  • What you are seeing is the difference between old-style and new-style classes. [See this answer](https://stackoverflow.com/a/54873/8316315) for more details. – Florian Weimer Oct 01 '18 at 14:03
  • An object is very generically *anything* that you can do `.something` on. `test` is an object (yes, it's a `class`, it's a *class object*), `'str'` is an object, *everything* is an object. An *instance* is specifically an object constructed from a class. – deceze Oct 01 '18 at 14:03
  • 1
    Do yourself a favour: Use Python 3. The object model changed a bit in between, which makes a few things clearer, among other improvements and new features. – Ulrich Eckhardt Oct 01 '18 at 14:04
  • thanks all for the feedback. i will update to python 3 and look at the link provided. – pradeep Oct 01 '18 at 19:33

0 Answers0