python - 如何防止其他人向 Python 中的对象/类添加新属性?
问题描述
class Person:
pass
smith = Person()
smith.gender = "Female"
print(smith.gender) # prints 'Female' in console
这是我的问题,因为 Person 类不应该包含属性“gender”。
当人们尝试将此性别属性添加到对象时,我想抛出类似“类'Person'没有属性'gender'”的错误。我怎样才能做到这一点?
解决方案
推荐阅读
- python - Leetcode 相同的树
- javascript - 如何将数据从兄弟组件传递给组件反应
- c# - 修复:将字符串中的第一个单词和其余单词分开(indexof 问题)
- javascript - 如何使用一个 json 对象嵌套或继承模板?使用小胡子 Js
- css - div之间出现意外的空白,仅在谷歌浏览器中
- sockets - 我的帖子 tidtcpclient 中的错误在哪里?
- react-native - 使用 Authoriztion 标头反应本机 axios 返回 400
- c - 为什么输出后有一个额外的百分号?
- postgresql - 当另一个值更改时如何重置自动递增的值?
- node.js - 几次成功迭代后,React Native 应用程序无法访问树莓派 3B 上的 Node.js 服务器