Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'friday,': 0.07; 'override': 0.07; 'enum': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'instance.': 0.09; 'message- id:@stoneleaf.us': 0.09; 'itself.': 0.11; '__new__': 0.16; 'instances,': 0.16; 'length:': 0.16; 'metaclass': 0.16; 'metaclasses': 0.16; 'third,': 0.16; 'two,': 0.16; 'user-defined': 0.16; 'wrote:': 0.16; '(in': 0.18; 'changes': 0.20; 'class,': 0.22; 'ones.': 0.22; 'defined': 0.23; '2015': 0.23; 'header:In- Reply-To:1': 0.24; 'previously': 0.24; 'second': 0.24; 'header :User-Agent:1': 0.26; '~ethan~': 0.29; 'function': 0.30; "can't": 0.32; 'skip:[ 10': 0.32; 'creating': 0.32; 'implement': 0.32; 'operate': 0.32; 'point': 0.33; 'class': 0.33; 'third': 0.33; 'instances': 0.33; 'subject:?': 0.34; 'needed': 0.34; 'to:addr :python-list': 0.35; 'instance': 0.35; 'created': 0.36; 'subject:" ': 0.36; 'received:10': 0.37; 'subject:: ': 0.37; 'pm,': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'behavior': 0.61; 'email addr:gmail.com': 0.64; 'received:10.1.10': 0.84; 'utc-7,': 0.84; 'ethan': 0.91; 'furman': 0.91 Date: Fri, 29 May 2015 14:39:44 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: What is considered an "advanced" topic in Python? References: <33b7ab62-8cc2-4a27-b229-64c94bce16b6@googlegroups.com> <656488c4-e706-4fea-ab45-3d1454e35a7f@googlegroups.com> In-Reply-To: <656488c4-e706-4fea-ab45-3d1454e35a7f@googlegroups.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432935608 news.xs4all.nl 2913 [2001:888:2000:d::a6]:47417 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91504 On 05/29/2015 02:06 PM, sohcahtoa82@gmail.com wrote: > On Friday, May 29, 2015 at 10:18:29 AM UTC-7, Ethan Furman wrote: >> >> Metaclasses change the way a class behaves. >> >> For example, the new (in 3.4) Enum class uses a metaclass. >> >> class SomeEnum(Enum): >> first = 1 >> second = 2 >> third = 3 >> >> The metaclass changes normal class behavior to: >> >> - support iterating: list(SomeEnum) --> [SomeEnum.first, SomeEnum.second, SomeEnum.third] >> - support a length: len(SomeEnum) --> 3 >> - not allow new instances to be created: --> SomeEnum(1) is SomeEnum(1) # True >> >> -- >> ~Ethan~ > > Regarding the first two, you can implement __iter__ and __len__ functions to create that functionality, though those functions would operate on an instance of the class, not the class itself. Hence the need for a metaclass, as the point is to operate on the class, not the instance. > As for the third, can't you override the __new__ function to make attempts to create a new instance just return a previously created instance? Yes. In the case of Enum, however, it takes any user-defined __new__, which is needed for creating the original instances, and replaces it with a __new__ that only returns the already defined ones. -- ~Ethan~