Python Forum

Full Version: Function calls inside a Class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Try
    before = self.countfiles()
(Mar-05-2017, 06:16 PM)zivoni Wrote: [ -> ]Try
    before = self.countfiles()
Thanks! I shoud have known...