Python将类和主程序分成不同的文件

时间:2015-03-20 00:24:20

标签: python class

我正在学习Python中的面向对象编程技术。创建了一个越来越长,越来越复杂的矢量类,我想通过创建一个单独的文件将我的类与主程序分开。随后可以在需要时将该类导入主程序。

这是包含类向量

的文件vector_class.py
class vector(object):

    def __init__(self, x=None, y=None, z=None, angle=None):

        if angle == None:
            self.x, self.y, self.z = x, y, z
        if angle != None:
            if angle == "rad":
                self.r, self.theta, self.phi = x, y, z
            if angle == "deg":
                self.r = x
                self.theta = y * 2. * pi / 360.
                self.phi = z * 2. * pi / 360.
            self.x = self.r * sin(self.theta) * cos(self.phi)
            self.y = self.r * sin(self.theta) * sin(self.phi)
            self.z = self.r * cos(self.theta)

    def show(self):
        print "[", self.x, "\t", self.y, "\t", self.z, "]"

    def write(self):
        file.write("[" + str(self.x) + ",\t" + str(self.y) + ",\t" + str(self.z) + "]")
    def write_sph(self):
        file.write("[" + str(self.mag()) + ",\t" + str(self.gettheta()) + ",\t" + str(self.getphi()) + "]")
    def swrite(self):
        file.write(str(self.x) + "\t" + str(self.y) + "\t" + str(self.z))
    def swrite_sph(self):
        file.write(str(self.mag()) + "\t" + str(self.gettheta()) + "\t" + str(self.getphi()))

    def getx(self):
        return self.x
    def gety(self):
        return self.y
    def getz(self):
        return self.z

    def setx(self, x):
        self.x = x
    def sety(self, y):
        self.y = y
    def setz(self, z):
        self.z = z

    def square(self):
        return self.x*self.x + self.y*self.y + self.z*self.z
    def mag(self):
        return sqrt(self.square())

    def gettheta(self):
        return arccos(self.z / self.mag())
    def getphi(self):
        return arctan2(self.y, self.x) # sign depends on which quadrant the coordinates are in

    def add(self, v):
        v_sum = vector(self.getx() + v.getx(), self.gety() + v.gety(), self.getz() + v.getz())
        return v_sum
    def minus(self, v):
        v_minus = vector(self.getx() - v.getx(), self.gety() - v.gety(), self.getz() - v.getz())
        return v_minus
    def diff(self, v):
        v_diff = vector(abs(self.getx() - v.getx()), abs(self.gety() - v.gety()), abs(self.getz() - v.getz()))
        return v_diff
    def inc(self, const): 
        v_new = vector(self.getx() + const, self.gety() + const, self.getz() + const)
        return v_new
    def times(self, const):
        v_new = vector(self.getx() * const, self.gety() * const, self.getz() * const)
        return v_new
    def div(self, const):
        v_new = vector(self.getx() / const, self.gety() / const, self.getz() / const)
        return v_new
    def sprod(self, v):
        ans = self.getx() * v.getx() + self.gety() * v.gety() + self.getz() * v.getz()
        return ans
    def vprod(self, v):
        v_new = vector()
        v_new.setx(self.gety() * v.getz() - self.getz() * v.gety())
        v_new.sety(self.getz() * v.getx() - self.getx() * v.getz())
        v_new.setz(self.getx() * v.gety() - self.gety() * v.getx())
        return v_new

这是包含导入类向量

的主程序的文件section2.py
from numpy import *
from vector_class import vector

v0 = vector(0., 0., 0.)
v1 = vector(-1.3054, 7.4033, 2.7362)
v2 = vector(8., 70., 100., "deg")
v3 = vector(8., 1.2217, 1.7453, "rad")

file = open("tasks.txt", 'w')

file.write("task 1 : method to print to file \n\n")
file.write("v0 = ")
v0.write()
file.write("\nv1 = ")
v1.write()

file.write("\n\ntask 2 : spherical polar constructor \n\n")
file.write("v2 = ")
v2.write()
file.write("\nv3 = ")
v3.write()

file.write("\n\ntask 3 : accessing individual coordinates \n\n")
file.write("v1 x component = " + str(v1.getx()) + "\n")
file.write("v1 y component = " + str(v1.gety()) + "\n")
file.write("v1 z component = " + str(v1.getz()) + "\n")

file.write("\ntask 4 : square and magnitude of the three vector \n\n")
file.write("v1 squared = " + str(v1.square()) + "\n")
file.write("v1 magnitude = " + str(v1.mag()) + "\n")

file.write("\ntask 5 : calculating spherical polar angles \n\n")
file.write("v1 spherical = ")
v1.write_sph()
file.write("\nv2 spherical = ")
v2.write_sph()
file.write("\nv3 spherical = ")
v3.write_sph()

file.write("\n\ntask 6 : modifying individual coordinates \n\n")
file.write("v0 = ")
v0.write()
v0.setx(7.77), v0.sety(8.88), v0.setz(9.99)
file.write("\nv0 = ")
v0.write()

file.write("\n\ntask 7 : adding vectors the hard way\n\n")
v4 = vector()
file.write("v0 + v1 = ")
v4.setx(v0.getx() + v1.getx())
v4.sety(v0.gety() + v1.gety())
v4.setz(v0.getz() + v1.getz())
v4.write()

file.write("\n\ntask 8 : overloading the add operator\n\n")
v4 = v0.add(v1)
file.write("v0 + v1 = ")
v4.write()

file.write("\n\ntask 9 : overloading the minus and increment operators \n\n")
v5 = v0.minus(v1)
file.write("v0 - v1 = ")
v5.write()
v6 = v0.inc(3.)
file.write("\nv0 += 3. = ")
v6.write()

file.write("\n\ntask 10 : scaling a vector \n\n")
v7 = v0.times(2.)
file.write("v0 * 2 = ")
v7.write()
v8 = v0.div(2.)
file.write("\nv0 / 2 = ")
v8.write()

file.write("\n\ntask 11 : the scalar product \n\n")
file.write("v0 . v1 = " + str(v0.sprod(v1)))

file.write("\n\ntask 12 : the vector product \n\n")
v9 = v0.vprod(v1)
file.write("v0 x v1 = ")
v9.write()

file.write("\n\ntask13 : triple product test \n\n")
file.write("v0 . (v0 x v1) = " + str(v0.sprod(v0.vprod(v1))))

file.close()
exit()

当我运行主程序时,我收到错误TypeError: descriptor 'write' requires a 'file' object but received a 'str',但如果所有代码都在一个文件中,一切正常。为什么我会收到错误,如何让类和主程序使用两个单独的文件?

2 个答案:

答案 0 :(得分:1)

filebuilt-in name,因此命名变量file可能会破坏某些内容。 file未在vector.py中定义,因此您正在调用文件对象的write()函数,但您将其称为静态函数,因此它不是&n #39;工作。我同意@roippi关于添加文件对象作为vector构造函数的参数:

def __init__(self, f, x=None, y=None, z=None, angle=None)

我在这里重命名为f以避免我提到的第一个问题,但你可以称之为其他问题。您只需要立即进行查找和替换,替换" file"用" f"在vector.py。

答案 1 :(得分:0)

def write(self):
    file.write("[" + str(self.x) + ",\t" + str(self.y) + ",\t" + str(self.z) + "]")

您指的是此方法正文中的全局file。全局每个模块,而非整个解释器实例。因此,当事物处于不同的模块中时,python不知道如何解析这个全局名称查找。

我建议重写你的vector构造函数以接收文件句柄(或指向文件的字符串)并将其存储在实例变量中。