0

したがって、1つのクラスStudentを持つ1つのパッケージstudentがあり、そのパッケージの外側にmain.pyがあり、Studentの例のオブジェクトを作成しようとしています

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()

どんな助けでも大歓迎です。

4

1 に答える 1

4

クラスはコードの外部で定義されていますが、クラスをインポートする必要があります。main.py と student.py が同じフォルダーにあるとします。

学生.py

class Student:
    Id=""

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


main.py

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()
于 2013-10-13T05:11:50.923 に答える