2

If I have this code:

class Fish(object):
    pass
class Salmon(Fish):
    pass

Is Salmon a object from class Fish? Which one is instance? What is x = Salmon() called?

0

1 Answer 1

2

Let me start simple: In Python everything is an object.

But I guess you're confusing "object from class" (I guess you meant "instance of class" there?) with "inheritance":

class Fish(object):
    pass

Creates a class Fish that inherits from object. Likewise:

class Salmon(Fish):
    pass

creates a class Salmon that inherits from Fish.

Both aren't "instances" in the normal sense. They are in fact "instances": they are both instances of type (which in turn subclasses object) the base metaclass for all classes but that's probably a bit too much to go into. Checkout metaclasses in Python if you want to know more.

What actually creates an instance is x = Salmon(): This creates an instance of Salmon.

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

2 Comments

Thx dude I get it now!!
@TeofanusGary No problem. Please don't forget to upvote all helpful answers and to accept the most helpful one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.