-2

Here is my code:

class fs_sync(object):

    def check_source(self, id):
        src = fb.get('/sources', id)
        return src

    def main():
        ...
        src = check_source(data['source_id'])

    if __name__ == "__main__":
        main()

I can't get the check_source function to be recognized. I get global name 'check_source' is not defined error. If I call it using self.check_source then I get an error global name 'self' is not defined.

How can call a simple function within a class?

MoreScratch
  • 2,933
  • 6
  • 34
  • 65
  • 7
    What is going on with your `def main` inside your class? That is quite odd. Is that intentional? It is hard to tell you with certainty what your problem is, because the fact you have that main in there is really raising other concerns. – idjaw Feb 04 '18 at 01:22
  • 4
    ... you don't put `if __name__ == "__main__":` in a class... – juanpa.arrivillaga Feb 04 '18 at 01:23
  • So a few things. One, is `, id')` actually what your code says? Two, is this your code's actual indentation? – Spencer D Feb 04 '18 at 01:23
  • 1
    Why does this class even exist? Python isn't Java; you don't need to stick absolutely everything in a class. – user2357112 Feb 04 '18 at 01:26
  • That is the actual indentation. I have it a class because I am going to call it in other modules. Sorry id' is not in the code. I will edit it. If I don't put main in the class I get `NameError: name 'main' is not defined`. – MoreScratch Feb 04 '18 at 01:30
  • `check_source` isn't a function; it's an instance method, and it must be invoked with an instance of `fs_sync`. – chepner Feb 04 '18 at 01:31
  • 3
    "I have it a class because I am going to call it in other modules" - that is not a reason to write a class. Classes are for defining new kinds of objects; if you are not trying to define a new kind of object, you shouldn't be writing a class. – user2357112 Feb 04 '18 at 01:32
  • 1
    If you can deal without the class, keep in mind that functions are import-able : `from import check_source` – JacobIRR Feb 04 '18 at 01:39

2 Answers2

2

Firstly, I wouldn't recommend that a method named "main".

Then, If class of fs_sync needs the main, you should write as following

def fs_main(self):
    ...
    src = self.check_source(data['source_id'])

Else, maybe you can create a dir or .py named utils or utils.py to contain your common function. Like as follow

in utils.py

    def for_fs_main(fs_sync):
        ...
        src = fs_sync.check_source(data['source_id'])

in your code

from utils import for_fs_main

class fs_sync(object):

    def check_source(self, id):
        src = fb.get('/sources', id)
        return src


if __name__ == "__main__":
    fss = fs_sync()
    for_fs_main(fss)
Yang MingHui
  • 380
  • 4
  • 14
  • There's nothing in PEP 8 that recommends against a method named "main"...? – Ned Batchelder Feb 04 '18 at 02:20
  • yeah, my mistake. It's a more common sense,i will edit it Immediately @NedBatchelder – Yang MingHui Feb 04 '18 at 02:25
  • @NedBatchelder there is nothing in PEP8 stating you shouldn't name a __function__ `main()` (in this case), nor anything about naming a method `main()`. Source: https://www.python.org/dev/peps/pep-0008/ – the_constant Feb 04 '18 at 03:36
0

@bernie yum is correct, but he missed couple points. When you call a function in a class, you say self.functionName(parameters) instead of functionName(parameters). In the function name you need to add self, but it is unnecessary to add it to the class during class creation.

If you do fb.get('/sources', id) you need to import fb first. Based on the code you've given us, I am not sure whether or not you have imported it.

It is good coding practice to add an else to an if.

Also generally you use an __init__, but it is not necessary, it depends on what you are trying to do.

You're code should look like:

import fb    

class fs_sync(object):
    def check_source(self, id):
        src = fb.get('/sources', id)
        return src

    def main(self):
        ...
        src = self.check_source(data['source_id'])

if __name__ == "__main__":
    main()
ds_secret
  • 338
  • 3
  • 18