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
#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


Messages In This Thread
RE: newbie question - can't make code work - by deanhystad - Oct-19-2023, 01:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I can't for the life of me get this basic If statement code to work CandleType1a 8 476 May-21-2024, 03:58 PM
Last Post: CandleType1a
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 824 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  hi need help to make this code work correctly atulkul1985 5 1,007 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  Newbie question about switching between files - Python/Pycharm Busby222 3 749 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,648 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,534 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 958 May-19-2023, 11:19 AM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 1,074 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  how to make bot that sends instagram auto password reset code kraixx 2 1,559 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  numpy newbie question bcwilly_ca 4 1,334 Feb-10-2023, 05:55 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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