每当我们在 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 是否有问题。
实现是一个巨大的链表。相册节点包含照片节点,照片节点包含标签节点
你绝对走在正确的道路上!PyCharm 的力量在于它的代码完成和类型提示。你没有做错什么,只是需要让 PyCharm 了解你的代码的结构,以便它为你提供帮助。这就是 Python 类型提示的用武之地。
让我们分解一下如何改进你的代码:
- 类型提示: 通过在你的函数参数和返回值中添加类型提示,PyCharm 可以准确地知道你正在使用什么类型。
```python from typing import Optional
class Album: def init (self, name: str, Photo: Optional['Photo'] = None, next: Optional['Album'] = None): self.name: str = name self.Photo: Optional['Photo'] = Photo self.next: Optional['Album'] = next
def __str__(self) -> str:
return "Album name is: " + self.name
class Photo: def init (self, name: str, caption: str, Tag: Optional['Tag'] = None, next: Optional['Photo'] = None): self.name: str = name self.caption: str = caption self.Tag: Optional['Tag'] = Tag self.next: Optional['Photo'] = next
def __str__(self) -> str:
return "Photo name is: " + self.name + " with caption: " + self.caption
class Tag: def init (self, type: str, info: str, next: Optional['Tag'] = None): self.name: str = type self.info: str = info self.next: Optional['Tag'] = next
def __str__(self) -> str:
return "Photo name is: " + self.name
def addPhoto(toEdit: 'Album', photoName: str, caption: str) -> None: if toEdit.Photo is None: toEdit.Photo = Photo(photoName, caption, None, None) else: tempPhoto: Optional['Photo'] = toEdit.Photo prev: Optional['Photo'] = None isFound: bool = False while tempPhoto is not None: if tempPhoto.name.lower() == photoName.lower(): isFound = True break prev = tempPhoto tempPhoto = tempPhoto.next
if isFound is False and prev is not None:
prev.next = Photo(photoName, caption, None, None)
else:
print("Photo " + photoName + " already exists in " + toEdit.name)
def deletePhoto(toEdit: 'Album', photoName: str) -> Optional[bool]: if toEdit.Photo is not None and 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: Optional['Photo'] = toEdit.Photo.next if toEdit.Photo is not None else None Photo_prev: Optional['Photo'] = None
while Photo is not None:
if Photo.name in photoName:
if Photo_prev is not None:
Photo_prev.next = Photo.next
return True
Photo_prev = Photo
Photo = Photo.next
print("Removed photo: " + photoName + " from " + toEdit.name)
return False
```
-
前向引用: 当你在类定义内部引用尚未定义的类时,Python 需要知道。你可以通过将类名放在字符串中来做到这一点,例如
'Photo'
。 -
可选类型: 当一个变量可以是
None
或特定类型时,使用Optional[type]
。
通过这些更改,PyCharm 将能够:
-
提供准确的代码完成建议,显示
pPtr
上可用的字段和方法。 -
检测类型错误,例如尝试将字符串分配给应该是一个
Photo
对象的变量。 - 使你的代码更易于阅读和理解,尤其是对于大型项目。
一个例子:
在你应用了类型提示后,当你输入
pPtr.
时,PyCharm 会立即显示
name
、
caption
、
Tag
和
next
作为可用的选项,因为 PyCharm 现在知道
pPtr
是一个
Photo
对象。
其他提示:
- 考虑使用 PyCharm 的内置类型检查器(例如 MyPy)来在运行代码之前捕获潜在的类型错误。
- 阅读更多关于 Python 类型提示的信息: https://docs.python.org/3/library/typing.html
希望这对你有帮助!
标签:python,pycharm,python-typing From: 24684954