Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Effects of caching frequently used objects, was Re: Explaining names vs variables in Python Date: Wed, 02 Mar 2016 10:12:48 +0100 Organization: None Lines: 70 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 49IUsOjP7XePj5q+Wb5DMgNIBA1aHTVgxks9Pj2vzxPQ== 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; 'cache': 0.05; 'sys': 0.05; 'instance.': 0.09; 'integers': 0.09; 'none.': 0.09; 'objects.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'variables,': 0.09; 'python': 0.10; 'argument': 0.15; 'interpreter': 0.15; 'properly': 0.15; 'dio': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; 'names.': 0.22; 'import': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'developers': 0.26; 'header:X -Complaints-To:1': 0.26; 'linux': 0.26; 'behaviour': 0.29; 'consistency': 0.29; 'expose': 0.29; 'objects': 0.29; 'similar': 0.33; 'done': 0.35; 'false': 0.35; 'identity': 0.35; 'something': 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'names': 0.38; 'someone': 0.38; 'does': 0.39; 'rather': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'hello,': 0.40; 'some': 0.40; 'save': 0.60; 'behavior': 0.61; 'subject: ': 0.61; 'more': 0.63; 'reuse': 0.66; 'decided': 0.66; 'therefore': 0.67; 'targeted': 0.70; 'salvatore': 0.84; 'versions)': 0.84; 'lot,': 0.95 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9cac.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103849 Salvatore DI DIO wrote: > Hello, > > I know Python does not have variables, but names. > Multiple names cant then be bound to the same objects. > > So this behavior > >>>> b = 234 >>>> v = 234 >>>> b is v > True > > according to the above that is ok > > > > But where is the consistency ? if I try : > >>>> v = 890 >>>> w = 890 >>>> v is w > False > > It is a little difficult to explain this behavior to a newcommer in Python > > Can someone give me the right argument to expose ? You should not bother with object identity for objects other than None. Some small integers are used a lot, e. g. Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.getrefcount(0) 606 >>> sys.getrefcount(1) 918 >>> sys.getrefcount(256) 31 >>> sys.getrefcount(-1) 51 therefore as an optimization the Python developers decided to put -5...256 (actual range may vary across interpreter versions) into a cache and reuse them rather than build a new object for every instance. This may save both time and memory, but is otherwise irrelevant. Something similar is done for strings: >>> a = "hello" >>> b = "hello" >>> a is b True >>> a = "hello, world" >>> b = "hello, world" >>> a is b False But: >>> a = "hello, world"; b = "hello, world" >>> a is b True Again this is an optimization (mostly targeted at attribute names) which should not affect the behaviour of a properly written Python program.