Python Forum
Function calls inside a Class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Function calls inside a Class (/thread-2296.html)



Function calls inside a Class - kerzol81 - Mar-05-2017

Hi,

Could you help me out a bit?
I don't understand why I cant call a function inside a class.

Here is the code I wrote so far:

import os
import glob
import time


folder = '.'

class Sc:


   def __init__(self, name):
       self.name = name

   def printmessage(self):
       print('*' * 5 + ' WATCHDOG ' + '*' * 5)

   def getlastwav(self):
       ''' returns last wav file in the folder '''
       newest = max(glob.iglob('*.[Ww][Aa][Wv]'), key=os.path.getctime)
       return newest
   def countfiles(self):
       ''' returns how many files in the folder '''
       file_count = len([name for name in os.listdir(folder) if os.path.isfile(name)])
       return file_count

   def checkfornewwav(self):
       before = countfiles()  # this function cannot be called inside the class.
       print(before)


s1 = Sc('first')
s1.checkfornewwav()
Why isn't that function is available inside the class?
Thy Pycharm says: NameError: name 'countfiles' is not defined


RE: Function calls inside a Class - zivoni - Mar-05-2017

Try
    before = self.countfiles()



RE: Function calls inside a Class - kerzol81 - Mar-05-2017

(Mar-05-2017, 06:16 PM)zivoni Wrote: Try
    before = self.countfiles()
Thanks! I shoud have known...