<small id='WVb9s'></small><noframes id='WVb9s'>

    1. <legend id='WVb9s'><style id='WVb9s'><dir id='WVb9s'><q id='WVb9s'></q></dir></style></legend>

      • <bdo id='WVb9s'></bdo><ul id='WVb9s'></ul>

    2. <i id='WVb9s'><tr id='WVb9s'><dt id='WVb9s'><q id='WVb9s'><span id='WVb9s'><b id='WVb9s'><form id='WVb9s'><ins id='WVb9s'></ins><ul id='WVb9s'></ul><sub id='WVb9s'></sub></form><legend id='WVb9s'></legend><bdo id='WVb9s'><pre id='WVb9s'><center id='WVb9s'></center></pre></bdo></b><th id='WVb9s'></th></span></q></dt></tr></i><div id='WVb9s'><tfoot id='WVb9s'></tfoot><dl id='WVb9s'><fieldset id='WVb9s'></fieldset></dl></div>
        <tfoot id='WVb9s'></tfoot>
      1. 从单独文件中的类创建对象

        时间:2023-09-13
      2. <i id='s2IA6'><tr id='s2IA6'><dt id='s2IA6'><q id='s2IA6'><span id='s2IA6'><b id='s2IA6'><form id='s2IA6'><ins id='s2IA6'></ins><ul id='s2IA6'></ul><sub id='s2IA6'></sub></form><legend id='s2IA6'></legend><bdo id='s2IA6'><pre id='s2IA6'><center id='s2IA6'></center></pre></bdo></b><th id='s2IA6'></th></span></q></dt></tr></i><div id='s2IA6'><tfoot id='s2IA6'></tfoot><dl id='s2IA6'><fieldset id='s2IA6'></fieldset></dl></div>

            <tbody id='s2IA6'></tbody>

          <small id='s2IA6'></small><noframes id='s2IA6'>

          1. <tfoot id='s2IA6'></tfoot>
              • <bdo id='s2IA6'></bdo><ul id='s2IA6'></ul>
                <legend id='s2IA6'><style id='s2IA6'><dir id='s2IA6'><q id='s2IA6'></q></dir></style></legend>

                  本文介绍了从单独文件中的类创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我已经完成了几个关于 Python 的教程,并且我知道如何定义类,但我不知道如何使用它们.例如,我创建了以下 file (car.py):

                  I have done several tutorials on Python and I know how to define classes, but I don't know how to use them. For example I create the following file (car.py):

                  class Car(object):
                      condition = 'New'
                      def __init__(self,brand,model,color):
                          self.brand = brand
                          self.model = model
                          self.color = color
                  
                      def drive(self):
                          self.condition = 'Used'
                  

                  然后我创建另一个文件 (Mercedes.py),我想从 Car 类中创建一个对象 Mercedes:

                  Then I create another file (Mercedes.py), where I want to create an object Mercedes from the class Car:

                  Mercedes = Car('Mercedes', 'S Class', 'Red')
                  

                  ,但我得到一个错误:

                  NameError: name 'Car' is not defined
                  

                  如果我在创建实例(汽车)的同一个文件中创建实例(对象),我没有问题:

                  If I create an instance (object) in the same file where I created it (car), I have no problems:

                  class Car(object):
                      condition = 'New'
                      def __init__(self,brand,model,color):
                          self.brand = brand
                          self.model = model
                          self.color = color
                  
                      def drive(self):
                          self.condition = 'Used'
                  
                  Mercedes = Car('Mercedes', 'S Class', 'Red')
                  
                  print (Mercedes.color)
                  

                  哪些打印:

                  Red
                  

                  所以问题是:如何从同一个包(文件夹)中不同文件的类创建对象?

                  So the question is: How can I create an object from a class from different file in the same package (folder)?

                  推荐答案

                  在你的 Mercedes.py 中,你应该按如下方式导入 car.py 文件(只要因为这两个文件在同一个目录):

                  In your Mercedes.py, you should import the car.py file as follows (as long as the two files are in the same directory):

                  import car
                  

                  那么你可以这样做:

                  Mercedes = car.Car('Mercedes', 'S Class', 'Red')  #note the necessary 'car.'
                  

                  或者,你可以这样做

                  from car import Car
                  
                  Mercedes = Car('Mercedes', 'S Class', 'Red')      #no need of 'car.' anymore
                  

                  这篇关于从单独文件中的类创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何访问由 `pip --user` 安装的软件包? 下一篇:“需要 Python 版本 2.7,但在注册表中找不到&quo

                  相关文章

                  最新文章

                  <i id='jxVI5'><tr id='jxVI5'><dt id='jxVI5'><q id='jxVI5'><span id='jxVI5'><b id='jxVI5'><form id='jxVI5'><ins id='jxVI5'></ins><ul id='jxVI5'></ul><sub id='jxVI5'></sub></form><legend id='jxVI5'></legend><bdo id='jxVI5'><pre id='jxVI5'><center id='jxVI5'></center></pre></bdo></b><th id='jxVI5'></th></span></q></dt></tr></i><div id='jxVI5'><tfoot id='jxVI5'></tfoot><dl id='jxVI5'><fieldset id='jxVI5'></fieldset></dl></div>

                        <bdo id='jxVI5'></bdo><ul id='jxVI5'></ul>
                      <tfoot id='jxVI5'></tfoot>
                      <legend id='jxVI5'><style id='jxVI5'><dir id='jxVI5'><q id='jxVI5'></q></dir></style></legend>
                    1. <small id='jxVI5'></small><noframes id='jxVI5'>