Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Is there any reason to introduce this intermediate variable (sz)? Date: Wed, 18 Nov 2015 05:02:13 -0500 Lines: 54 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de HqIKpXM+rDMf1qTHEhBrnw94Vw95oVZT29P00X295xNw== 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; 'sequences.': 0.07; 'calculating': 0.09; 'derived': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'variance': 0.09; 'python': 0.10; 'jan': 0.11; '..)': 0.16; '3:51': 0.16; 'fits': 0.16; 'naming': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'singleton': 0.16; 'subject:)?': 0.16; 'subject:variable': 0.16; 'wrote:': 0.16; 'shape': 0.18; 'variable': 0.18; 'not,': 0.22; 'arrays': 0.22; 'constant': 0.22; 'correctly.': 0.22; 'function,': 0.22; 'int,': 0.22; 'tuples': 0.22; 'seems': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sequence': 0.27; 'function': 0.28; 'idea': 0.28; 'once,': 0.29; 'array': 0.29; 'code': 0.30; 'useful': 0.33; 'curious': 0.33; 'int': 0.33; 'similar': 0.33; 'could': 0.35; 'quite': 0.35; 'something': 0.35; 'asking': 0.35; 'but': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'signature': 0.37; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'space': 0.40; 'skip:n 10': 0.62; 'above,': 0.63; 'more': 0.63; 'different': 0.63; 'subject:there': 0.66; 'below.': 0.66; 'fact,': 0.67; 'subject:any': 0.84; 'subject:this': 0.85; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-71-185-227-36.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: 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: , Xref: csiph.com comp.lang.python:98955 On 11/17/2015 3:51 PM, fl wrote: > n_iter = 50 > sz = (n_iter,) # size of array > x = -0.37727 > z = np.random.normal(x,0.1,size=sz) > > Q = 1e-5 # process variance > > # allocate space for arrays > xhat=np.zeros(sz) > P=np.zeros(sz) > > > I learn Python now and the above code seems from an experienced author. > The curious thing to me is the variable 'sz'. 'sz' is a name and in the program above, it is a constant tuple derived from constant int n_iter. Since the tuple is used more than once, calculating it just once and naming it is a good idea. > I have check np.zeros(shape, ..) Reading about the the signature of functions that you use or read about is a good idea and quite normal. > shape : int or sequence of ints > The introduced 'sz' is a tuple. and tuples are sequences. > If n_iter function is similar to a constant in C, n_iter is a named constant, not a function, as you note below. > I don't see the reason for 'sz'. I gave a reason for it existig above, but I think you are asking about its use in the particular location. > In fact, 'n_iter' is an int, which fits the below > > np.zeros(shape) > > correctly. Could you see something useful with variable 'sz'? I would presume until I tried it that np.zeros(50) and np.zeros((50,)) are different (have a different shape). If they are not, then the use of a singleton tuple is confusing, whether or not the tuple is given a name. -- Terry Jan Reedy