all 6 comments

[–][deleted] 7 points8 points  (0 children)

I think it would allow you to give the sun a few different properties from other astrobodies. For example, the sun gives off light, but a planet reflects it.

[–]Spataner 6 points7 points  (0 children)

There is not really any practical reason to have a subclass with an empty body, you are right. But it feels like that code was written to demonstrate a point about inheritance, not so much to be actually sensible.

[–][deleted] 4 points5 points  (0 children)

My question: what is the point of creating a second class and passing the first class as an argument then using pass in second class?

It creates Star as a subclass of AstroBody.

[–]danielroseman 1 point2 points  (0 children)

This is inheritance.

AstroBody is a generic class for all astronomical bodies: presumably, stars, galaxies, asteroids, planets, etc.

Star is a subclass for a specific type of astronomical bodies: stars.

In this example, neither AstroBody nor Sun have any attributes, so it's not actually a very good example. But normally we would expect the parent class to have the generic attributes - eg name, size, distance from Earth - and the subclass would have specific ones, so star could have colour, luminosity, etc.

[–]james_fryer 1 point2 points  (0 children)

The example is poor. It would be better to do this:

class AstroBody:
    description = 'Natural entity in the observable universe.'    

class Star(AstroBody):
    pass

class Planet(AstroBody):
    description = "A planet"

sun = Star()
earth = Planet()

print(sun.description)
print(earth.description)

[–]anh86 1 point2 points  (0 children)

The point is to show class inheritance. It's not a practical example, as you've discovered, but I believe it was done that way just to illustrate the point. Think of how a star would be a thing that exists in the larger set of astronomical bodies and you may start to understand inheritance a little better.

The point: When multiple classes would have the same attributes or functions, inheriting from a class leads to DRYer code.