1

I started learning Python and got stuck at classes (with no prior programming background or knowledge).

Is an "object" a synonym for "an instance of the class"?

Thank you.

stgeorge
  • 483
  • 3
  • 7
  • 16
  • 1
    usually, in python everything is an object, even the classes. – cmd Jun 03 '13 at 21:02
  • 1
    yes, when you are referring to class objects. but in general, object can be of any type – karthikr Jun 03 '13 at 21:03
  • 1
    @cmd right, and classes are also instances of class `type` so that is true anyway. – kirelagin Jun 03 '13 at 21:03
  • 1
    this discussion may be of some use to you http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python – cmd Jun 03 '13 at 21:05
  • Thank you cmd. I will bookmark this page for some further reading, it definitely has some useful stuff. For now I will satisfy myself just by knowing the objects and instances of class are the same thing. Thank you guys, all of you. I love the stackoverflow. The quickest replies in the whole web, and each time got by experts. Wonderful community! – stgeorge Jun 03 '13 at 21:21

2 Answers2

7

depends. Typically, I would say no. object is the base class of all new style classes (all classes in python3.x):

class Foo(object):
    ...

As such, I would say that object is more the most basic type of most class instances. So, if I create a Foo:

f = Foo()
print isinstance(f,object)  #True

We see that f is an instance of the object type.


However, there are a lot of informal terms which get thrown around and object is one of them. In some contexts, you'll see people saying that 'anything is an object'. Basically what they mean by that is that you can assign anything to a new name:

bar = Foo  #Now bar is an "alias" for Foo.

We can do this assignment because Foo is an "object" (lazy vernacular).

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thank you for such a detailed reply Mgilson. But it seems it's too advanced for me. Still appreciate the effort. – stgeorge Jun 03 '13 at 21:14
1

Yes, and by the way everything in Python is an object.

Here goes pretty good explanation in Dive Into Python.

In addition, here is the link to pretty good video... It might be a little overwhelming for novice user. However, "everything is an object" explanation is pretty good: Python Epiphanies

PSS
  • 5,561
  • 5
  • 28
  • 30
  • I think I won't be able to sleep because of this sentence. :) Damn, when I started learning it, it was suppose to be user friendly language. Now I am totally confused. – stgeorge Jun 03 '13 at 21:05
  • 1
    @stgeorge Exactly. It's user-friendly because it's very simple. And its simplicity comes from the fact that there are no special cases. Everything is simply an object. _Everything_. – kirelagin Jun 03 '13 at 21:06
  • 1
    @stgeorge I make it even simplier to everything is a list object :) – PSS Jun 03 '13 at 21:07
  • 1
    @stgeorge Here is the link to pretty good video... It might be a little overwhelming for novice user. However, "everything is an object" explanation is pretty good: http://youtu.be/Pi9NpxAvYSs – PSS Jun 03 '13 at 21:09
  • 1
    +0. This answer could be improved by incorporating a quote or two from the linked material. – Steven Rumbalski Jun 03 '13 at 21:09
  • 1
    @StevenRumbalski How is it better if you copy and past copyrighted stuff? – PSS Jun 03 '13 at 21:13
  • @PSS: Selected quoting with attribution of copyrighted material is acceptable as fair use. I think a couple of sentences from the second paragraph of section *2.4.2. What's an Object?* would be helpful. – Steven Rumbalski Jun 03 '13 at 21:19
  • @PSS: See also Stack Overflow's [How to answer](http://stackoverflow.com/questions/how-to-answer): **Provide context for links.** "A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." – Steven Rumbalski Jun 03 '13 at 21:24
  • @StevenRumbalski Thank you so much for your involvement in my answer. I will make sure I follow above mentioned guidelines, and be the best I can be next time. – PSS Jun 03 '13 at 21:27