2013-11-15 8 views
6

が定義されていませんが、私のexample.pyファイルです:NameError:グローバル名 'myExample2' はここで#モジュール

class myClass: 
    def __init__(self, number): 
     self.number = number 
    def myExample(self): 
     result = myExample2(self.number) - self.number 
     print(result) 
    def myExample2(num): 
     return num*num 

私はexample.pyファイルを実行すると、私が持っている:

from myimport import * 
def main(): 
    myimport2 = myimport(10) 
    myimport2.myExample() 

if __name__ == "__main__": 
    main() 

そしてここmyimport.pyファイルです次のエラー:

NameError: global name 'myExample2' is not defined 

どうすれば修正できますか?

+2

あなたは 'myExample2(self、num)'を必要とし、それを 'self.myExample2()'と呼んでいます。 –

答えて

6

あなたのコードを簡単に修正しました。

from myimport import myClass #import the class you needed 

def main(): 
    myClassInstance = myClass(10) #Create an instance of that class 
    myClassInstance.myExample() 

if __name__ == "__main__": 
    main() 

そしてmyimport.py

class myClass: 
    def __init__(self, number): 
     self.number = number 
    def myExample(self): 
     result = self.myExample2(self.number) - self.number 
     print(result) 
    def myExample2(self, num): #the instance object is always needed 
     #as the first argument in a class method 
     return num*num 
7

私はあなたのコードで2つのエラーを参照してください。あなたはmyExample2を定義する際selfを追加する必要が

  • self.myExample2(...)として myExample2を呼び出す必要が

    1. を:def myExample2(self, num): ...
    0

    あなたは、インスタンスを作成する必要がありますmyClassクラスのインスタンスではなく、モジュール全体のインスタンスではありません(そして、私は変数名をあまりひどいものに編集します)。

    from myimport import * 
    def main(): 
        #myobj = myimport.myClass(10) 
        # the next string is similar to above, you can do both ways 
        myobj = myClass(10) 
        myobj.myExample() 
    
    if __name__ == "__main__": 
        main() 
    
    0

    他の回答が正しいですがmyExample2()はメソッドであることの必要性が本当に存在する場合、私は疑問に思います。

    def myExample2(num): 
        return num*num 
    
    class myClass: 
        def __init__(self, number): 
         self.number = number 
        def myExample(self): 
         result = myExample2(self.number) - self.number 
         print(result) 
    

    それとも、あなたがきれいにあなたの名前空間を維持したい場合は、メソッドとしてそれを実装するが、それは@staticmethodとして、selfを必要としないよう:あなたは同様にそれがスタンドアロン実装できる

    def myExample2(num): 
        return num*num 
    
    class myClass: 
        def __init__(self, number): 
         self.number = number 
        def myExample(self): 
         result = self.myExample2(self.number) - self.number 
         print(result) 
        @staticmethod 
        def myExample2(num): 
         return num*num 
    
    0

    まず、私はalKidの答えに同意します。これは本当に答えよりも質問に対するコメントですが、私はコメントする評判はありません。

    私のコメント:

    エラーが発生するグローバル名がmyImportないmyExample2

    説明されています

    私はPython 2.7で生成され、完全なエラーメッセージは次のとおりです。

    Message File Name Line Position  
    Traceback    
        <module> C:\xxx\example.py 7  
        main C:\xxx\example.py 3  
    NameError: global name 'myimport' is not defined 
    

    私がobsを追跡しようとしていたときにこの質問が見つかりました自分のコードで "グローバル名が定義されていません"というエラーを修正してください。質問のエラーメッセージが間違っているので、私はもっと混乱してしまいました。私が実際にコードを実行して実際のエラーを見たとき、それはすべて意味がありました。

    これは、このスレッドが私の同じ問題を抱えていることを知る誰も妨げないことを願っています。私よりも評判の高い人がこれをコメントにしたり質問を修正したい場合は、気軽にご利用ください。

    +0

    質問に答えないと私を罵倒したすべての人に、私はお詫びします。 ** TLDR:質問のコードサンプルはエラーを生成しません。質問は答えられない!** – Dennis

    関連する問題