{"id":270145,"date":"2021-01-11T03:47:06","date_gmt":"2021-01-10T19:47:06","guid":{"rendered":"http:\/\/4563.org\/?p=270145"},"modified":"2021-01-11T03:47:06","modified_gmt":"2021-01-10T19:47:06","slug":"classmethod-%e5%8f%af%e4%bb%a5%e6%9d%a5%e8%b0%83%e7%94%a8%e7%b1%bb%e7%9a%84%e5%b1%9e%e6%80%a7%ef%bc%8c%e7%b1%bb%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%8c%e5%ae%9e%e4%be%8b%e5%8c%96%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=270145","title":{"rendered":"classmethod \u53ef\u4ee5\u6765\u8c03\u7528\u7c7b\u7684\u5c5e\u6027\uff0c\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  classmethod \u53ef\u4ee5\u6765\u8c03\u7528\u7c7b\u7684\u5c5e\u6027\uff0c\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fanqieipnet <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        classmethod \u53ef\u4ee5\u6765\u8c03\u7528\u7c7b\u7684\u5c5e\u6027\uff0c\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u4eca\u5929\u756a\u8304\u52a0\u901f\u5c31\u6765\u8bb2\u4e00\u4e0b\u3002<\/p>\n<p>\u3000\u3000 classmethod()<\/p>\n<p>\u3000\u3000 classmethod \u4fee\u9970\u7b26\u5bf9\u5e94\u7684\u51fd\u6570\u4e0d\u9700\u8981\u5b9e\u4f8b\u5316\uff0c\u4e0d\u9700\u8981 self \u53c2\u6570\uff0c\u4f46\u7b2c\u4e00\u4e2a\u53c2\u6570\u9700\u8981\u662f\u8868\u793a\u81ea\u8eab\u7c7b\u7684 cls \u53c2\u6570\uff0c\u53ef\u4ee5\u6765\u8c03\u7528\u7c7b\u7684\u5c5e\u6027\uff0c\u7c7b\u7684\u65b9\u6cd5\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7b49\u3002<\/p>\n<p>\u3000\u3000 In [66]: class Student():<\/p>\n<p>\u3000\u3000&#8230;: def __init__(self,id,name):<\/p>\n<p>\u3000\u3000&#8230;: self.id = id<\/p>\n<p>\u3000\u3000&#8230;: self.name = name<\/p>\n<p>\u3000\u3000&#8230;: def __repr__(self):<\/p>\n<p>\u3000\u3000&#8230;: return &#8216;id = &#8216;+self.id +&#8217;, name = &#8216;+self.name<\/p>\n<p>\u3000\u3000&#8230;: @classmethod<\/p>\n<p>\u3000\u3000&#8230;: def f(cls):<\/p>\n<p>\u3000\u3000&#8230;: print(cls)<\/p>\n<p>\u3000\u3000 complie()<\/p>\n<p>\u3000\u3000\u5c06\u5b57\u7b26\u4e32\u7f16\u8bd1\u6210 python \u80fd\u8bc6\u522b\u6216\u53ef\u4ee5\u6267\u884c\u7684\u4ee3\u7801\uff0c\u4e5f\u53ef\u4ee5\u5c06\u6587\u5b57\u8bfb\u6210\u5b57\u7b26\u4e32\u518d\u7f16\u8bd1\u3002<\/p>\n<p>\u3000\u3000 In [74]: s = &#8220;print(&#8216;helloworld&#8217;)&#8221;<\/p>\n<p>\u3000\u3000 In [75]: r = compile(s,&#8221;&#8221;, &#8220;exec&#8221;)<\/p>\n<p>\u3000\u3000 In [76]: r<\/p>\n<p>\u3000\u3000 Out[76]: at 0x0000000005DE75D0, file &#8220;&#8221;, line 1&gt;<\/p>\n<p>\u3000\u3000 In [77]: exec(r)<\/p>\n<p>\u3000\u3000 helloworld<\/p>\n<p>\u3000\u3000 complex()<\/p>\n<p>\u3000\u3000\u521b\u5efa\u4e00\u4e2a\u590d\u6570<\/p>\n<p>\u3000\u3000 In [81]: complex(1,2)<\/p>\n<p>\u3000\u3000 Out[81]: (1+2j)<\/p>\n<p>\u3000\u3000 delattr()<\/p>\n<p>\u3000\u3000\u5220\u9664\u5bf9\u8c61\u7684\u5c5e\u6027<\/p>\n<p>\u3000\u3000 In [87]: delattr(xiaoming,&#8217;id&#8217;)<\/p>\n<p>\u3000\u3000 In [88]: hasattr(xiaoming,&#8217;id&#8217;)<\/p>\n<p>\u3000\u3000 Out[88]: False<\/p>\n<p>\u3000\u3000 dict()<\/p>\n<p>\u3000\u3000\u521b\u5efa\u6570\u636e\u5b57\u5178<\/p>\n<p>\u3000\u3000 In [92]: dict()<\/p>\n<p>\u3000\u3000 Out[92]: {}<\/p>\n<p>\u3000\u3000 In [93]: dict(a=&#8217;a&#8217;,b=&#8217;b&#8217;)<\/p>\n<p>\u3000\u3000 Out[93]: {&#8216;a&#8217;: &#8216;a&#8217;, &#8216;b&#8217;: &#8216;b&#8217;}<\/p>\n<p>\u3000\u3000 In [94]: dict(zip([&#8216;a&#8217;,&#8217;b&#8217;],[1,2]))<\/p>\n<p>\u3000\u3000 Out[94]: {&#8216;a&#8217;: 1, &#8216;b&#8217;: 2}<\/p>\n<p>\u3000\u3000 In [95]: dict([(&#8216;a&#8217;,1),(&#8216;b&#8217;,2)])<\/p>\n<p>\u3000\u3000 Out[95]: {&#8216;a&#8217;: 1, &#8216;b&#8217;: 2}<\/p>\n<p>\u3000\u3000 dir()<\/p>\n<p>\u3000\u3000\u4e0d\u5e26\u53c2\u6570\u65f6\u8fd4\u56de\u5f53\u524d\u8303\u56f4\u5185\u7684\u53d8\u91cf\uff0c\u65b9\u6cd5\u548c\u5b9a\u4e49\u7684\u7c7b\u578b\u5217\u8868;\u5e26\u53c2\u6570\u65f6\u8fd4\u56de\u53c2\u6570\u7684\u5c5e\u6027\uff0c\u65b9\u6cd5\u5217\u8868\u3002<\/p>\n<p>\u3000\u3000 In [96]: dir(xiaoming)<\/p>\n<p>\u3000\u3000 Out[96]:<\/p>\n<p>\u3000\u3000[&#8216;__class__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__delattr__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__dict__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__dir__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__doc__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__eq__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__format__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__ge__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__getattribute__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__gt__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__hash__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__init__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__init_subclass__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__le__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__lt__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__module__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__ne__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__new__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__reduce__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__reduce_ex__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__repr__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__setattr__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__sizeof__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__str__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__subclasshook__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;__weakref__&#8217;,<\/p>\n<p>\u3000\u3000&#8217;name&#8217;]<\/p>\n<p>\u3000\u3000 divmod()<\/p>\n<p>\u3000\u3000\u5206\u522b\u53d6\u5546\u548c\u4f59\u6570<\/p>\n<p>\u3000\u3000 In [97]: divmod(10,3)<\/p>\n<p>\u3000\u3000 Out[97]: (3, 1)      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>classmethod \u53ef\u4ee5\u6765\u8c03\u7528&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/270145"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=270145"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/270145\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=270145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=270145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=270145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}