Databricks Python 创建和使用客户 class

Databricks Pyhton creating and using Customer class

我创建了一个客户 class,我想在我的主代码中使用它。

Model/customer.py

###
#
# File: Model/customer.py
# Version 1.0.0
#
###


class Customer:
    """ A class representing an customer. """

    __name = None
    __name_abbreviated = None


    # Initialize Model class ----------------------------------------------------------------
    def __init__(self, name='', name_abbreviated=''):
        """ Initializes customer class with blank values. """
        self.__name= name
        self.__name_abbreviated = name_abbreviated

现在我想在我的主笔记本中使用客户 class,但这似乎很难:

主笔记本:

# COMMAND ----------

# MAGIC %run "Model/customer"

customer = Customer('Big Corporation', 'BC')


# COMMAND ----------

这给我错误:

NameError: name 'Customer' is not defined
---------------------------------------------------------------------------
NameError                                 Traceback
     3 # MAGIC %run "Model/customer"
---> 5 customer = Customer('Big Corporation', 'BC')


NameError: name 'Customer' is not defined

我一般使用相对路径: %运行 ./Model/cusomter.py

并且您应该将 %运行 和客户变量赋值拆分到不同的单元格中。