我刚刚切换到Pycharm,我很高兴它提供了所有警告和提示,以改进我的代码.除了这个我不明白的:
This inspection detects shadowing names defined in outer scopes.
我知道从外部作用域访问变量是不好的做法但是遮蔽外部作用域的问题是什么?
这是一个例子,Pycharm给了我警告信息:
data = [4, 5, 6]
def print_data(data): # <-- Warning: "Shadows 'data' from outer scope
print data
print_data(data)
Run Code Online (Sandbox Code Playgroud) 我试图找到一个很好的来源,解释为什么global在python(以及一般的编程)中使用被认为是不好的做法.有人可以指点我或解释一下吗?
我是Python的新手并且遵循教程.list教程中有一个例子:
example = list('easyhoss')
Run Code Online (Sandbox Code Playgroud)
现在,在教程中,example= ['e','a',...,'s'].但在我的情况下,我得到以下错误:
>>> example = list('easyhoss')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
请告诉我我错在哪里.我搜索了这个,但它是不同的.
我知道永远不要使用内置函数名作为变量标识符.
但有没有理由不将它们用作属性或方法标识符?
例如,在我自己的类中编写my_object.id = 5或定义实例方法是否安全dict?
python namespaces naming-conventions reserved-words python-3.x
假设我的数据库的这一项:
{"_id" : ObjectID("526fdde0ef501a7b0a51270e"),
"info": "foo",
"status": true,
"subitems : [ {"subitem_id" : ObjectID("65sfdde0ef501a7b0a51e270"),
//more},
{....}
],
//more
}
Run Code Online (Sandbox Code Playgroud)
我想找到(或 find_one,没关系)带有"subitems.subitem_id" : xxx.
我已经尝试过以下方法。它们都返回一个空列表。
from pymongo import MongoClient,errors
from bson.objectid import ObjectId
id = '65sfdde0ef501a7b0a51e270'
db.col.find({"subitems.subitem_id" : id } ) #obviously wrong
db.col.find({"subitems.subitem_id" : Objectid(id) })
db.col.find({"subitems.subitem_id" : {"$oid":id} })
db.col.find({"subitems.subitem_id.$oid" : id })
db.col.find({"subitems.$.subitem_id" : Objectid(id) })
Run Code Online (Sandbox Code Playgroud)
然而在 mongoshell 中,这个是有效的:
find({"subitems.subitem_id" : { "$oid" : "65sfdde0ef501a7b0a51e270" } })
Run Code Online (Sandbox Code Playgroud) 谁能详细解释一下这是什么意思?f for f in...例如
list = [f for f in os.listdir(os.getcwd()) if os.path.isdir(f)]
print(list)
Run Code Online (Sandbox Code Playgroud)
我了解基本 for 循环的语法,但我已经多次看到这种类型的事情,并且发现它非常令人困惑。我一直在搜索,我所能找到的只是有关格式化的信息。我大约一个月前才开始学习Python,并且仍在学习中。感谢您的任何帮助!
使用保留关键字或内置函数作为变量/属性名称通常被视为不好的做法。然而,SQLALchemy教程充满了名为 的属性的示例id。
直接从教程开始
>>> class User(Base):
... __tablename__ = "user_account"
...
... id: Mapped[int] = mapped_column(primary_key=True)
... name: Mapped[str] = mapped_column(String(30))
... fullname: Mapped[Optional[str]]
...
... addresses: Mapped[List["Address"]] = relationship(
... back_populates="user", cascade="all, delete-orphan"
... )
...
... def __repr__(self) -> str:
... return f"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})"
Run Code Online (Sandbox Code Playgroud)
为什么不建议使用它id_来代替,至少对于PEP 8中的关键字是推荐的?
这是我的模特
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
import uuid
class PiO(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # surrogate
person = models.ForeignKey(Person, on_delete=models.PROTECT, max_length=25, blank=True)
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT) # for the various organization types
object_id = models.UUIDField(primary_key=False, default=uuid.uuid4, editable=False) # the uuid of the specific org
content_object = GenericForeignKey('content_type', 'object_id')
Run Code Online (Sandbox Code Playgroud)
这是我的追溯
AttributeError: 'UUIDField' object has no attribute 'uuid4'.
Run Code Online (Sandbox Code Playgroud)
请注意,这是特定引用object_id字段,而不是 uuid(pk)字段.作为测试,我注释掉了object_id字段.我并没有得到一个错误,不具有OBJECT_ID字段,检查12线走了,给到一个新的错误.
我用谷歌搜索了确切的短语并得到了
No results found for "AttributeError: 'UUIDField' object has no attribute 'uuid4'".
Run Code Online (Sandbox Code Playgroud)
我所做的看起来与我的文档一致. …
每当我用作id变量名称时,我的 IDE 都会以与其他变量不同的颜色显示该术语。这是预期的还是 IDE 的某些功能(我正在使用 vs code)或者我不应该将其用作id变量?
我在运行代码时没有遇到任何问题。只有颜色的变化让我好奇。
python ×8
python-3.x ×2
coding-style ×1
django ×1
django-1.9 ×1
for-loop ×1
list ×1
mongodb ×1
namespaces ×1
pycharm ×1
pymongo ×1
python-2.7 ×1
side-effects ×1
sqlalchemy ×1