On Wed, 16 Aug 2017 18:02:48 -0700, Sayth Renshaw wrote: > 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
I think you shod also print tree_frog.type not frog.type you create the object tree_frog which is of type frog. Although not a fault it is also recommended that classes have a capital first letter to make them more visible* * this is imply style guidance & not compulsory see pep8 for more detail when you feel ready for it. -- <Crow-> who gives a shit about US law <jim> anyone living in the US. -- https://mail.python.org/mailman/listinfo/python-list