2017-12-12 7 views
0

私はパッケージングスクリプトをテストし、将来の使用のためにそれらをインストールしようとしています。私はスクリプト 'my_script.py'を作成し、 python docs\setup.py developでインストールしました。うまくインストールされているので、うまくいきました。このコードは、これを含んでいた:私は '名前は定義されていません' Pythonで私が作成し、 'distribution'でインストールしたパッケージを参照するとき

from my_script import test 

tool = (1, 2, 3, 4, 5, 6) 

test_print(self, tool) 

、それが戻っています:私は間違って

Traceback (most recent call last): 
    File "bin\test2.py", line 5, in <module> 
    test_print(self, tool) 
NameError: name 'test_print' is not defined 

何をやっているの

class test(object): 

    def test_print(self, tool): 

     for i in tool: 
      print i 

は、その後、私は言うスクリプトを作成しましたか?

+0

インポートが正常に動作すると仮定すると、それでも 'test()。test_print(tool) 'でなければなりません。 'test_print'は' test'クラスのインスタンスメソッドです。また、 'self'引数を渡さず、クラス内でのみ使用されます。 – orangeInk

答えて

1

定義test_printは、テストクラスのメソッドです。だから、あなたはそれを使用する前にオブジェクトをインスタンス化する必要があります。

from my_script import test 

tool = (1, 2, 3, 4, 5, 6) 

testObj = test() 
testObj.test_print(tool) 

それ以外の場合は、静的としてメソッドを定義する@staticmethodデコレータを追加することも可能です。

class test(object): 
    @staticmethod 
    def test_print(tool): 
     for i in tool: 
      print i 

from my_script import test 

tool = (1, 2, 3, 4, 5, 6) 
test.test_print(tool) 
+0

ありがとう、それは完璧な意味を持ち、魅力のように動作します! – Cdhippen

関連する問題