Python Forum
newbie question - can't make code work - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: newbie question - can't make code work (/thread-40953.html)



newbie question - can't make code work - tronic72 - Oct-19-2023

Hi,

New to python and doing a course by Mosh. I have a tutorial with the following code that will NOT work no matter what I do.

class Animal:

class Mammal(Animal):

m = Mammal ()
print (isinstance(m, Mammal))
print (isinstance(m, Animal))
The output should be a boolean. Can someone point out what Im missing please?

PS the error is :

Error:
File "/Users/mark/PycharmProjects/complete python mastery/main.py", line 1191 class Mammal(Animal): ^ IndentationError: expected an indented block after class definition on line 1189 Process finished with exit code 1



RE: newbie question - can't make code work - deanhystad - Oct-19-2023

The message is pretty clear. Python expects you to indent like this:
class Animal:
 
    class Mammal(Animal):
 
m = Mammal ()
print (isinstance(m, Mammal))
print (isinstance(m, Animal))
But now you get this eror:
Error:
m = Mammal () IndentationError: expected an indented block after class definition on line 3
So the problem is not indenting so much as python expects the code following the class declaration to be indented.

You could follow the class declaration by an actual class definition.
class Animal:
    def some_method(self, value):
        self.a = value
 
class Mammal(Animal):
    def some_other_method(self, value):
        self.b = value
 
m = Mammal ()
print (isinstance(m, Mammal))
print (isinstance(m, Animal))
Or you could use a placeholder.
class Animal:
    pass
 
class Mammal(Animal):
    pass
 
m = Mammal ()
print (isinstance(m, Mammal))
print (isinstance(m, Animal))
Or a docstring.
class Animal:
    """Kingdom of living things that are not plants."""
 
class Mammal(Animal):
    """An animal that produces milk."""
 
m = Mammal ()
print (isinstance(m, Mammal))
print (isinstance(m, Animal))



RE: newbie question - can't make code work - tronic72 - Oct-22-2023

Hi deanhystad.

Thanks so much for your detailed explanation. It turns on that tutor had used the following syntax:

class Mammal(Animal): ...

But the three dots after the colon were really tight together and the IDE he used displayed them in a very light grey colour.

I wasn't aware of this notation until now which as i understand it is used as an alternative to

class Mammal(Animal):
pass

Thanks again for taking the time to provide a great explanation.

T