Python-我新创建的类实例丢失

Python-我新创建的类实例丢失,python,Python,我正在准备Python考试,我正在试验类和类方法,在下面的代码中我创建了一个类实例,但我无法检索它,即使它显示在类“dict 代码如下: class Watch: instances = [] def __init__(self, model): self.model = model self.eng = "" self.__class__.instances.append(self.model) @classmethod def crea

我正在准备Python考试,我正在试验类和类方法,在下面的代码中我创建了一个类实例,但我无法检索它,即使它显示在类“dict 代码如下:

class Watch:
   instances = []

def __init__(self, model):
    self.model = model
    self.eng = ""
    self.__class__.instances.append(self.model) 

@classmethod
def create_new_watch(cls, model, eng):
    if eng.isalnum() and len(eng) < 40:
        watch = cls(model)
        watch.eng = eng
    else: 
        raise ValueError("Invalid engraving")
    if len(watch.eng) > 0:
        print("Your new watch model {} was created".format(watch.model))
        print("Your new watch will have the following engraving {}".format(watch.eng))
    else:
        print("Your new watch model {} was created".format(watch.model))
    return watch

object1 = Watch.create_new_watch("model S", "Newengraving")

def create_object(object2, model, eng):
    try: 
        object2 = Watch.create_new_watch(model, eng)
        print(object2)
        print(isinstance(object2, Watch))
    except ValueError as e:
        print(e)
    else:
        print("the object was successfully created")

create_object("Watch4", "modello T", "Anotherengraving")

#this works, object 1 is an instance of the class Watch
print(isinstance(object1, Watch))

#but here it raises an exception saying that object 2 is not specified
print(isinstance(object2, Watch))

#but 2 instances of the class are present, so why isinstance(object2, Watch) raises an exception?
print(Watch.__dict__)
'''
课堂观察:
实例=[]
定义初始化(自我,模型):
self.model=model
self.eng=“”
self.\u类\u.instances.append(self.model)
@类方法
def创建新手表(cls、型号、发动机):
如果eng.isalnum()和len(eng)<40:
手表=cls(型号)
watch.eng=eng
其他:
提升值错误(“无效雕刻”)
如果len(watch.eng)>0:
打印(“您的新手表型号{}已创建”。格式(watch.model))
打印(“您的新手表将具有以下雕刻{}”。格式(watch.eng))
其他:
打印(“您的新手表型号{}已创建”。格式(watch.model))
回程表
object1=手表。创建新手表(“S型”、“新雕刻”)
def create_对象(对象2,模型,工程):
尝试:
object2=手表。创建新的手表(型号,英语)
打印(对象2)
打印(iInstance(对象2,手表))
除ValueError为e外:
打印(e)
其他:
打印(“对象已成功创建”)
创建对象(“Watch4”、“modellot”、“anotheringraving”)
#这是可行的,对象1是类Watch的一个实例
打印(iInstance(对象1,手表))
#但在这里它引发了一个异常,表示未指定对象2
打印(iInstance(对象2,手表))
#但是该类有两个实例,那么为什么isinstance(object2,Watch)会引发异常呢?
打印(手表、录音)
'''
我的object1就在那里,
isinstance(object1,Watch)
返回
true
,但是object2上的相同函数会引发一个错误,即使我可以看到该类中已经创建了第二个类实例


my object2是在哪个名称下创建的?

您的
object2
仅存在于您的功能范围内。

您的
object2
似乎不是全局的,而是本地的
创建对象()