Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.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; 'sys': 0.07; "'python": 0.09; 'doing?': 0.09; 'instances.': 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; 'least)': 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; '{})': 0.16; 'wrote:': 0.18; 'basically': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'equivalent': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'waste': 0.30; 'code': 0.31; '>>>>': 0.31; 'obscure': 0.31; 'class': 0.32; 'classes': 0.35; 'there': 0.35; 'instances': 0.36; 'useful': 0.36; 'employee': 0.37; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; "you'll": 0.62; 'name': 0.63; 'between': 0.67; 'readers': 0.68; 'surprise': 0.74 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 11:37:04 +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> 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407058641 news.xs4all.nl 2909 [2001:888:2000:d::a6]:42905 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75584 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', ) 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', ) >>> 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: >>> import sys >>> sys.getsizeof(employee) 976 >>> class Employee: pass ... >>> employee = Employee() >>> employee.name = "John Doe" >>> employee.position = "Python programmer" >>> sys.getsizeof(employee) 64