Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects vs instance in python

Tags:

c++

python

object

In C++ there are just objects and classes, where objects are instances of classes.

In Python, a class definition (i.e., the body of a class) is called an object. And, the object in C++ is called instance in python.

Check this

Am I wrong?

EDIT : Actually can someone explain with example difference of object vs instance

EDIT : In python, everything will inherit from object class & hence everything is an object (i.e object of object class).

A Class is also an object (i.e object of object class).

Instance is the name used to call the object of any class.(a.k.a c++ object).

Please refer this

like image 505
Jibin Avatar asked Jun 16 '11 07:06

Jibin


1 Answers

In Python, a class definition (i.e., the body of a class) is called an object

Actually, this is still called a class in Python. That's why you define it like this:

class Foo(object):
    pass

The class keyword is used because the result is still called a class.

The word object is in parentheses to show that Foo is derived from the class called object. Don't be confused -- any existing class could be used here; more than one, in fact.

The reason you usually derive classes from object is a historical accident but probably is worth a detail. Python's original object implementation treated user-defined classes and built-in types as slightly different kinds of things. Then the language's designer decided to unify these two concepts. As a result, classes derived from object (or from a descendant of object) behave slightly differently from classes that are not derived from object and are called new-style classes. Old-style classes, on the other hand, were ones defined like this:

class Foo:
    pass

class Bar(Foo):
    pass

Note these do not inherit from object or from anything else that inherits from object. This makes them old-style classes.

When working with Python 2.x, your classes should almost always inherit from object, as the new-style objects are nicer to work with in several small but important ways.

To further confuse things, in Python 3.0 and later, there are no old-style classes, so you don't have to derive from object explicitly. In other words, all the above classes would be new-style classes in Python 3.x.

Now, back to the matter at hand. Classes are objects because everything is an object in Python. Lists, dictionaries, integers, strings, tuples... all of these are objects, and so are the building blocks of Python programs: modules, functions, and classes. You can create a class using the class keyword and then pass it to a function, modify it, etc. (For completeness, you can also create a class using the type() function.)

A class is a template for building objects, which are referred to as instances. This part you already know. You instantiate objects similar to calling a function, passing in the initial values and other parameters:

mylist = list("abc")   # constructs ["a", "b", "c"]

Behind the scenes, this creates an instance, then calls the new instance's __init__() method to initialize it. Since everything's an object in Python, instances of a class are also objects.

One last thing you might want to know is that just as classes are templates for building objects, so it is possible to have templates for building classes. These are called metaclasses. The base metaclass is called type (that is, an ordinary new-style class is an instance of type).

(Yes, this is the same type that I mentioned earlier can be used to create classes, and the reason you can call it to create classes is that it's a metaclass.)

To create your own metaclass, you derive it from type like so:

class mymeta(type):
    pass

Metaclasses are a fairly advanced Python topic, so I won't go into what you might use them for or how to do it, but they should make it clear how far Python takes the "everything's an object" concept.

like image 186
kindall Avatar answered Oct 11 '22 18:10

kindall