Python Forum
newbie question - can't make code work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie question - can't make code work
#1
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
deanhystad write Oct-19-2023, 01:19 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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))
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 704 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  hi need help to make this code work correctly atulkul1985 5 800 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  Newbie question about switching between files - Python/Pycharm Busby222 3 622 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,381 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 829 May-19-2023, 11:19 AM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 990 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  how to make bot that sends instagram auto password reset code kraixx 2 1,385 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  numpy newbie question bcwilly_ca 4 1,193 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,808 Jan-29-2023, 06:24 PM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020