0

1)Why we need to call class from Object ?

Can you provide some good example where calling from object is necessary . I found below one but not getting it. In Python 2.7 x this should look like below

class Foo:
    pass

class Bar(Foo):
    pass

2) I found that , in Python 3.x there is no need to derive explicitly from object ? What does this mean ? Does this mean we don't need to call class Bar(Foo) ? Hard to understand as a newbie. Please explain .

3) In Python 2.7

class Boat():
    pass

my_boat = Boat() #instantiate object 

my_boat2 = Boat() #instantiate object

print(my_boat)
print(my_boat2)

<main.Boat instance at 0x0000000008DFEC88>

<main.Boat instance at 0x0000000008DFECC8>

Running the same code in Python 3.x gives me

<main.Boat object at 0x000002406FF8E390>

<main.Boat object at 0x000002406FF8ECF8>

a) So Can I conclude that instance and Object are same in Python 3.x ? Although after going through many readings from different sources I understand that

Instance is just a blueprint of Class.

Class is also an Object . When we instantiate a class it's an object.

Everything in Python is an Object

Correct me in case my understanding is wrong. So following the above understanding it is quite hard to understand why Object and instance are the same thing. Please clear me on this .

0

1 Answer 1

1

I suggest you take a look at A Byte of Python, the chapter on Object Oriented Programming.

In Summary: A class is the blueprint by which objects are made. What you are doing in your first example is called inheritance. This is never required and completely optional.

class SchoolMember:
    '''Represents any school member.'''
    age = some_age
    pass


class Teacher(SchoolMember):
    '''Represents a teacher.'''
    pass


class Student(SchoolMember):
    '''Represents a student.'''
    pass

From a Byte of Python:

The SchoolMember class in this situation is known as the base class or the superclass. The Teacher and Student classes are called the derived classes or subclasses.

Now every teacher and every student will have some age. But again, it is not nessecary. You could simply do:

class Teacher():
    '''Represents a teacher.'''
    age = some_age
    pass


class Student():
    '''Represents a student.'''
    age = some_age
    pass

and get the same results without inheritance.

As for your question 3a:

An instance of a class is known as an object. In other languages creating an object and instantiating it are two distinct steps but in python these are done at the same time so an object will always be an instance as well.

Your current understanding has some flaws:

Instance is just a blueprint of Class.

As I said, a Class is a blueprint of an object.

Class is also an Object . When we instantiate a class it's an object.

An Object is also an Instance. When we create an object from a class it's automatically instantiated.

Everything in Python is an Object

This doesn't make sense at all. Everything in python is based on Objects, but that is something to worry about later when you have a deeper understanding.

I hope I could help you understand a bit better. I really suggest you take a look at A Byte of Python. It has a really good explanation of Object Oriented Programming.

Sign up to request clarification or add additional context in comments.

1 Comment

@Redme thanks a lot for putting the effort and explain the concept. I will accept your answer once I go through the suggested topic and understand the concept better . Till then keep contributing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.