创建另一个类python的对象

时间:2013-10-13 05:01:36

标签: python object

所以我有一个包学生和一个班级学生,我在该包外面有一个main.py,我正在尝试创建一个学生示例的对象

class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id

单独的文件main.py:

 def main():
        print("is workign")
        temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

虽然在代码之外定义了类,但是需要导入类。

例如:

# in student.py
class Student:
    Id=""

    def __init__(self, Id):
        self.Id = Id

# in main.py, assume main.py and student.py are in the same folder.
def main():
    from student import Student
    print("is workign")
    temp =  Student("50")  ## I want to create the object of class Student and send an attribute

if __name__ == '__main__':
    main()