Why is my class undefined?

Sayth Renshaw flebber.crue at gmail.com
Wed Aug 16 21:02:48 EDT 2017


On Thursday, 17 August 2017 09:03:59 UTC+10, Ian  wrote:
 wrote:
> > Morning
> >
> > I haven't ventured into classes much before. When trying to follow some examples and create my own classes in a jupyter notebook I receive an error that the class is undefined.
> >
> > So I created for practise a frog class
> >
> > class frog(object):
> >
> >     def __init__(self, ftype, word):
> >         self.ftype = ftype
> >         self.word = ftype
> >
> >     def jump(self):
> >         """
> >         Make frog jump
> >         """
> >         return "I am jumping"
> >
> >     if __name__ == "__main__":
> >         tree_frog = frog("Tree Frog", "Ribbitt")
> >         print(frog.ftype)
> >         print(frog.word)
> >
> >
> > I receive this error
> >
> > NameError                                 Traceback (most recent call last)
> > <ipython-input-1-609567219688> in <module>()
> > ----> 1 class frog(object):
> >       2
> >       3     def __init__(self, ftype, word):
> >       4         self.ftype = ftype
> >       5         self.word = ftype
> >
> > <ipython-input-1-609567219688> in frog()
> >      12
> >      13     if __name__ == "__main__":
> > ---> 14         tree_frog = frog("Tree Frog", "Ribbitt")
> >      15         print(frog.ftype)
> >      16         print(frog.word)
> >
> > NameError: name 'frog' is not defined
> >
> > what exactly am I doing wrong?
> 
> The if __name__ == "__main__" block is inside the class declaration
> block, so at the point that it runs the class has not been created
> yet. Try removing the indentation to place it after the class block
> instead.

Thank you that had me bugged I just couldn't see it.

Cheers

Sayth



More information about the Python-list mailing list