0

I'm having some trouble calling a function which is within a class in python.

Here is my folder hierarchy.
~/Code/program/main.py
~/Code/program/dc_functions/dcfunc.py
~/Code/program/dc_functions/init.py

Basically, I want to use a function from dcfunc.py inside of main.py. How would I do this?

Relevant contents of dcfunc.py:

import subprocess, string, os, sys
class dcfunc:
 #Create raw Audio track(Part of Dreamcast Disc format) + Burn track to disk.
 def __init__(self):
 self.self = "self" 
 def burnaudiotrack(device):
 **CODE***

Thanks for any suggestions!

asked May 13, 2011 at 23:57
1
  • 1
    Please, take a look to PEP 8. Also, self.self = "self" doesn't have sense. And def burnaudiotrack should have as first argument self. You are saying that you want to use a function from dcfunc.py, are you sure? Probably you want to call a method instead. Commented May 14, 2011 at 0:15

1 Answer 1

4

You need your init.py file to be named __init__.py

then use

from dc_functions.dcfunc import function_name

And you'll have acccess to the function.

answered May 14, 2011 at 0:00
Sign up to request clarification or add additional context in comments.

7 Comments

When I try to import it like that, then try to use it, I get a NameError. Do I just call it as if it were local? For example : function()
Yes. You're importing the name directly into the local namespace, so you can call it directly.
'from dc_functions.dcfunc import *' This is what I added into my main.py. When I try to call any function within it from main.py, the interpreter tells me the name is not defined. Maybe something in init.py is wrong. I have "import dcfunc" there, and nothing else.
File "./main.py", line 44, in <module> burn() NameError: name 'burn' is not defined
You have all of the functions in that file inside a class. In order for what I told you to work they have to be outside of the class. The class in that file doesn't make any sense. Probably you should just take all of the methods out of the class.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.