Python Forum
Function calls inside a Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function calls inside a Class
#1
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
Reply
#2
Try
    before = self.countfiles()
Reply
#3
(Mar-05-2017, 06:16 PM)zivoni Wrote: Try
    before = self.countfiles()
Thanks! I shoud have known...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 563 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  with open context inside of a recursive function billykid999 1 552 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  TimeOut a function in a class ? Armandito 1 1,587 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  asyncio calls within sync function ( Websocket on_open) orion67 0 1,375 Jan-16-2022, 11:00 AM
Last Post: orion67
  Calling a class from a function jc4d 5 1,756 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  a function common to methods of a class Skaperen 7 2,501 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Tuple generator, and function/class syntax quazirfan 3 3,762 Aug-10-2021, 09:32 AM
Last Post: buran
  Calls to Attributes of a Class SKarimi 3 3,340 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  How to make global list inside function CHANKC 6 2,981 Nov-26-2020, 08:05 AM
Last Post: CHANKC

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020