0

プログラミングのスキルを向上させるために、クラス オブジェクトを使用しています。3 つのファイル *.py があります。基本的な例で申し訳ありませんが、エラーがどこにあるかを理解するのに役立ちます:

/my_work_directory
   /core.py *# Contains the code to actually do calculations.*
   /main.py *# Starts the application*
   /Myclass.py *# Contains the code of class*

Myclass.py

class Point(object):
    __slots__= ("x","y","z","data","_intensity",\
                "_return_number","_classification")
    def __init__(self,x,y,z):
        self.x = float(x)
        self.y = float(y)
        self.z = float(z)
        self.data = [self.x,self.y,self.z]

   def point_below_threshold(self,threshold):
        """Check if the z value of a Point is below (True, False otherwise) a
            low Threshold"""
        return check_below_threshold(self.z,threshold)

core.py

def check_below_threshold(value,threshold):
    below = False
    if value - threshold < 0:
        below = not below
    return below

def check_above_threshold(value,threshold):
    above = False
    if value - threshold > 0:
        above = not above
    return above

main.py を設定すると

import os
os.chdir("~~my_work_directory~~") # where `core.py` and `Myclass.py` are located

from core import *
from Myclass import *

mypoint = Point(1,2,3)
mypoint.point_below_threshold(5)

私は得る:

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "Myclass.py", line 75, in point_below_threshold
    return check_below_threshold(self.z,threshold)
NameError: global name 'check_below_threshold' is not defined
4

2 に答える 2

1

他のモジュールの関数は、モジュールで自動的に表示されませんMyclass。それらを明示的にインポートする必要があります。

from core import check_below_threshold

または、coreモジュールをインポートして、それを名前空間として使用します。

import core

# ...
    return core.check_below_threshold(self.z,threshold)
于 2013-03-01T18:34:13.340 に答える
0

インポートがありません。関数を使用する場所にインポートする必要があります。つまり、core.py にも check_below_threshhold をインポートする必要があります。

于 2013-03-01T18:37:08.067 に答える