Why is my class undefined?

Ian Kelly ian.g.kelly at gmail.com
Wed Aug 16 19:02:50 EDT 2017


On Wed, Aug 16, 2017 at 4:48 PM, Sayth Renshaw <flebber.crue at gmail.com> 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.



More information about the Python-list mailing list