Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'sys': 0.07; 'ugly': 0.07; "'python": 0.09; 'differently.': 0.09; 'doing?': 0.09; 'instances.': 0.09; 'latter': 0.09; 'namespace': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; '"python': 0.16; 'doing,': 0.16; 'employee.': 0.16; 'least)': 0.16; 'merely': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:object': 0.16; 'subject:simple': 0.16; 'subject:type': 0.16; 'wow,': 0.16; '{})': 0.16; 'all.': 0.16; 'wrote:': 0.18; '(not': 0.18; 'trying': 0.19; 'basically': 0.19; 'items.': 0.19; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'equivalent': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'waste': 0.30; 'code': 0.31; 'consumption': 0.31; 'obscure': 0.31; 'class': 0.32; 'url:python': 0.33; '-----': 0.33; 'maybe': 0.34; 'classes': 0.35; 'but': 0.35; 'there': 0.35; 'instances': 0.36; 'useful': 0.36; 'url:org': 0.36; 'employee': 0.37; 'email addr:python.org': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'subject:': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'august': 0.61; 'simple': 0.61; "you'll": 0.62; 'name': 0.63; 're:': 0.63; 'kind': 0.63; 'more': 0.64; 'email name :python-list': 0.65; 'pays': 0.65; 'between': 0.67; 'readers': 0.68; 'surprise': 0.74; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Correct type for a simple "bag of attributes" namespace object Date: Sun, 03 Aug 2014 13:23:42 +0200 Organization: None References: <7ef67ccc-3fc3-47dd-b858-09ef3b57a497@googlegroups.com> <87r40zmkhr.fsf@elektro.pacujo.net> <53dd0717$0$29973$c3e8da3$5496439d@news.astraweb.com> <70e5f2f1-661f-40f2-bb7a-76e569c4d092@googlegroups.com> <1407057464.64980.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1407063072.46317.YahooMailNeo@web163803.mail.gq1.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb961.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407065042 news.xs4all.nl 2844 [2001:888:2000:d::a6]:38291 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75590 Albert-Jan Roskam wrote: > > > ----- Original Message ----- > >> From: Peter Otten <__peter__@web.de> >> To: python-list@python.org >> Cc: >> Sent: Sunday, August 3, 2014 11:37 AM >> Subject: Re: Correct type for a simple "bag of attributes" namespace >> object >> >> Albert-Jan Roskam wrote: >> >>> I find the following obscure (to me at least) use of type() useful >>> exactly for this "bag of attributes" use case: >>>>>> employee = type("Employee", (object,), {}) >>>>>> employee.name = "John Doe" >>>>>> employee.position = "Python programmer" >>>>>> employee.name, employee.position, employee >>> ('John Doe', 'Python programmer', > '__main__.Employee'>) >> >> Are you sure you know what you are doing? The above is equivalent to >> >>>>> class employee: >> ... name = "John Doe" >> ... position = "Python programmer" >> ... >>>>> employee.name, employee.position, employee >> ('John Doe', 'Python programmer', > '__main__.employee'>) >>>>> type(employee) >> >> >> Basically you are using classes as instances. While there is no >> fundamental difference between classes and instances in Python you'll >> surprise readers of your code and waste some space: > > Yes, I know that it is equivalent, but I have always found it kind of ugly > to use class() just to bundle a number of items. But that's what you are doing, you just spell it differently. > Like you are 'announcing > OOP' (not sure how to put this into words), and then it's merely a simple > bundle. Or maybe it's just me being silly, because it's even in the Python > tutorial: https://docs.python.org/2/tutorial/classes.html#odds-and-ends > >>>>> import sys >>>>> sys.getsizeof(employee) >> 976 >>>>> class Employee: pass >> ... >>>>> employee = Employee() >>>>> employee.name = "John Doe" >>>>> employee.position = "Python programmer" >>>>> sys.getsizeof(employee) >> 64 > Wow, I was not aware of that at all. So they are not equivalent after all. I think you are misunderstanding what I was trying to say; so to be explicit: >>> class Employee: pass ... >>> sys.getsizeof(Employee) 976 i. e. you have a per-class and per-instance memory consumption. The latter is smaller, so with regards to memory consumption instantiating only pays off when there is more than one employee.