每当我们需要Java中的新对象时,我们声明类型和名称,选择是否给它一个初始值。在Python中,我们不能这样做,因为我们不声明类型。在
我怎样才能避免这种情况,因为如果类型没有声明,我就没有得到任何代码完成提示。就像一个特定对象的字段或者我们可以调用的任何方法。。。在
class Album:
def __init__(self, name, Photo, next):
self.name = name
self.Photo = None
self.next = next
def __str__(self):
return "Album name is: " + self.name
class Photo:
def __init__(self, name, caption, Tag, next):
self.name = name
self.caption = caption
self.Tag = Tag
self.next = next
def __str__(self):
return "Photo name is: " + self.name + " with caption: " + self.caption
class Tag:
def __init__(self, type, info, next):
self.name = type
self.info = info
self.next = next
def __str__(self):
return "Photo name is: " + self.name
def addPhoto(toEdit, photoName, caption):
if isinstance(toEdit, Album):
if toEdit.Photo is None:
toEdit.Photo = Photo(photoName, caption, None, None)
else:
tempPhoto = toEdit.Photo
prev = None
isFound = False
while tempPhoto != None:
if tempPhoto.name.lower() == photoName.lower():
isFound = True
break
prev = tempPhoto
tempPhoto = tempPhoto.next
if isFound == False:
prev.next = Photo(photoName, caption, None, None)
else:
print("Photo " + photoName + " already exists in " + toEdit.name)
def deletePhoto(toEdit, photoName):
if isinstance(toEdit, Album):
if photoName in toEdit.Photo.name:
if toEdit.Photo.next is not None:
toEdit.Photo = toEdit.Photo.next
return True
else:
toEdit.Photo = None
return True
else:
Photo = toEdit.Photo.next
Photo_prev = None
while Photo is not None:
if Photo.name in photoName:
prev.next = Photo.next
prev = Photo
Photo = Photo.next
print("Removed photo: " + photoName + " from " + toEdit.name)
pPtr = album1.Photo
while (pPtr != None):
print(pPtr)
pPtr = pPtr.next
因此,每当我尝试执行pPtr = album1.Photo
并尝试访问pPtr
对象的任何字段时,PyCharm中没有任何建议。我要知道我是做错了还是PyCharm错了。在
实现是一个巨大的链接列表。相册节点包含包含标记节点的照片节点
您应该遵循以下intellij过程:
https://www.jetbrains.com/help/pycharm/type-hinting-in-product.html
示例:
这是动态类型语言的一个限制。类型的静态分析在某种程度上是可能的。所以不要期望像静态类型语言那样多的自动完成。在
首先,正如对您的问题的一位评论者所指出的,使用大写的变量名会使它们与类/类型混淆。如果你看看你的问题,你会发现它甚至混淆了Stackoverflow代码格式化程序。在
此外,在相册构造函数中,您使用局部参数屏蔽全局变量“Photo”。这可能会混淆PyCharm,尤其是当您执行以下操作时。所以对于下面的答案+测试,我将你的参数重命名为小写的照片,这样它就不会产生干扰。在
**编辑:**我确实找到了更好的方法。见“正确的道路”。在
第一次尝试
它的工作方式是使用PyCharm类型的推理。有关如何工作的详细信息,请参见here。在
下面是它工作的屏幕截图:
注意:我不建议这样做,因为这是一次黑客攻击。当我试图找出在PyCharm中是否有更好的方法来解决这个问题时,我遇到了你的问题。在
正确的方法
正确的方法是给PyCharm构造函数中的变量类型。本质上,将类型绑定从上面的方法中移动到构造函数中,作为docstring条目“type”的一部分。在
^{pr2}$相关问题 更多 >
编程相关推荐