{"id":687,"date":"2026-03-16T21:29:00","date_gmt":"2026-03-16T13:29:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=687"},"modified":"2026-07-14T21:45:41","modified_gmt":"2026-07-14T13:45:41","slug":"python%e5%b1%9e%e6%80%a7%e6%8f%8f%e8%bf%b0%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/16\/python%e5%b1%9e%e6%80%a7%e6%8f%8f%e8%bf%b0%e5%ba%94%e7%94%a8\/","title":{"rendered":"Python\u5c5e\u6027\u63cf\u8ff0\u5e94\u7528"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. \u60f0\u6027\u8ba1\u7b97<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class LazyProperty:<br> &nbsp; &nbsp;\"\"\"\u60f0\u6027\u52a0\u8f7d\u5c5e\u6027\uff1a\u53ea\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\u8ba1\u7b97\"\"\"<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;def __init__(self, func):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.func = func<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = func.__name__<br>\u200b<br> &nbsp; &nbsp;def __get__(self, instance, owner):<br> &nbsp; &nbsp; &nbsp; &nbsp;if instance is None:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self<br> &nbsp; &nbsp; &nbsp; &nbsp;value = self.func(instance)<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5c06\u7ed3\u679c\u7f13\u5b58\u5230\u5b9e\u4f8b\u7684\u5c5e\u6027\u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp;setattr(instance, self.name, value)<br> &nbsp; &nbsp; &nbsp; &nbsp;return value<br>\u200b<br>\u200b<br>class DataLoader:<br> &nbsp; &nbsp;def __init__(self, file_path):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.file_path = file_path<br>\u200b<br> &nbsp; &nbsp;@LazyProperty<br> &nbsp; &nbsp;def data(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;print(f\"\u6b63\u5728\u52a0\u8f7d\u6587\u4ef6: {self.file_path}\")<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<br> &nbsp; &nbsp; &nbsp; &nbsp;return &#91;1, 2, 3, 4, 5]<br>\u200b<br>\u200b<br>loader = DataLoader(\"data.txt\")<br>print(loader.data) &nbsp;# \u6b63\u5728\u52a0\u8f7d\u6587\u4ef6: data.txt<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &#91;1, 2, 3, 4, 5]<br>print(loader.data) &nbsp;# &#91;1, 2, 3, 4, 5] \u2014\u2014 \u4e0d\u518d\u52a0\u8f7d\uff0c\u76f4\u63a5\u4ece\u5c5e\u6027\u8bfb\u53d6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u7c7b\u578b\u68c0\u67e5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Typed:<br> &nbsp; &nbsp;\"\"\"\u5f3a\u5236\u7c7b\u578b\u68c0\u67e5\u7684\u63cf\u8ff0\u7b26\"\"\"<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;def __init__(self, expected_type):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.expected_type = expected_type<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = None<br>\u200b<br> &nbsp; &nbsp;def __set_name__(self, owner, name):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br>\u200b<br> &nbsp; &nbsp;def __get__(self, instance, owner):<br> &nbsp; &nbsp; &nbsp; &nbsp;if instance is None:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self<br> &nbsp; &nbsp; &nbsp; &nbsp;return instance.__dict__&#91;self.name]<br>\u200b<br> &nbsp; &nbsp;def __set__(self, instance, value):<br> &nbsp; &nbsp; &nbsp; &nbsp;if not isinstance(value, self.expected_type):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise TypeError(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f\"{self.name} \u5fc5\u987b\u662f {self.expected_type.__name__} \u7c7b\u578b\uff0c\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f\"\u800c\u4e0d\u662f {type(value).__name__}\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  )<br> &nbsp; &nbsp; &nbsp; &nbsp;instance.__dict__&#91;self.name] = value<br>\u200b<br>\u200b<br>class Student:<br> &nbsp; &nbsp;name = Typed(str)<br> &nbsp; &nbsp;age = Typed(int)<br> &nbsp; &nbsp;score = Typed(float)<br>\u200b<br> &nbsp; &nbsp;def __init__(self, name, age, score):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.name = name<br> &nbsp; &nbsp; &nbsp; &nbsp;self.age = age<br> &nbsp; &nbsp; &nbsp; &nbsp;self.score = score<br>\u200b<br>\u200b<br>s = Student(\"Alice\", 20, 85.5)<br># s.age = \"20\" &nbsp; &nbsp;  # TypeError! age \u5fc5\u987b\u662f int \u7c7b\u578b\uff0c\u800c\u4e0d\u662f str<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>1. \u60f0\u6027\u8ba1\u7b97 2. \u7c7b\u578b\u68c0\u67e5<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[54],"class_list":["post-687","post","type-post","status-publish","format-standard","hentry","category-python","category-back","tag-python"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=687"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":688,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/687\/revisions\/688"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}