When I write functions in Python, I typically need to pass quite a few variables to the function. Also, output of such functions contains more than a few variables. In order to manage this variables I/O, I resort to the dictionary datatype, where I pack all input variables into a dictionary to inject into a function and then compile another dictionary at the end of the function for returning to the main program. This of course needs another unpacking of the output dictionary.   dict_in = {'var1':var1,            'var2':var2,            'varn':varn}      def foo(dict_in):     var1 = dict_in['var1']     var2 = dict_in['var2']     varn = dict_in['varn']      """ my code """      dict_out = {'op1':op1,                 'op2':op2,                 'op_m':op_m}     return dict_out   As the list of variables grows, I suspect that this will be an inefficient approach to deal with the variables I/O.   Can someone suggest a better, more efficient and less error-prone approach to this practice?
The canonical way to return multiple values in languages that support it is often . Option: Using a tuple Consider this trivial example: def f(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return (y0, y1, y2)  However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you could keep tupling them, but it gets easy to forget which value is where. It's also rather ugly to unpack them wherever you want to receive them. Option: Using a dictionary The next logical step seems to be to introduce some sort of 'record notation'. In Python, the obvious way to do this is by means of a dict. Consider the following: def g(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return {'y0': y0, 'y1': y1 ,'y2': y2}  (Just to be clear, y0, y1, and y2 are just meant as abstract identifiers. As pointed out, in practice you'd use meaningful identifiers.) Now, we have a mechanism whereby we can project out a particular member of the returned object. For example, result['y0']  Option: Using a class However, there is another option. We could instead return a specialized structure. I've framed this in the context of Python, but I'm sure it applies to other languages as well. Indeed, if you were working in C this might very well be your only option. Here goes: class ReturnValue:   def __init__(self, y0, y1, y2):      self.y0 = y0      self.y1 = y1      self.y2 = y2  def g(x):   y0 = x + 1   y1 = x * 3   y2 = y0 ** y3   return ReturnValue(y0, y1, y2)  In Python the previous two are perhaps very similar in terms of plumbing - after all { y0, y1, y2 } just end up being entries in the internal __dict__ of the ReturnValue. There is one additional feature provided by Python though for tiny objects, the __slots__ attribute. The class could be expressed as: class ReturnValue(object):   __slots__ = [&quot;y0&quot;, &quot;y1&quot;, &quot;y2&quot;]   def __init__(self, y0, y1, y2):      self.y0 = y0      self.y1 = y1      self.y2 = y2  From the :  The __slots__ declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__ is not created for each instance.  Option: Using a  (Python 3.7+) Using Python 3.7's new dataclasses, return a class with automatically added special methods, typing and other useful tools: @dataclass class Returnvalue:     y0: int     y1: float     y3: int  def total_cost(x):     y0 = x + 1     y1 = x * 3     y2 = y0 ** y3     return ReturnValue(y0, y1, y2)  Option: Using a list Another suggestion which I'd overlooked comes from Bill the Lizard: def h(x):   result = [x + 1]   result.append(x * 3)   result.append(y0 ** y3)   return result  This is my least favorite method though. I suppose I'm tainted by exposure to Haskell, but the idea of mixed-type lists has always felt uncomfortable to me. In this particular example the list is -not- mixed type, but it conceivably could be. A list used in this way really doesn't gain anything with respect to the tuple as far as I can tell. The only real difference between lists and tuples in Python is that lists are , whereas tuples are not. I personally tend to carry over the conventions from functional programming: use lists for any number of elements of the same type, and tuples for a fixed number of elements of predetermined types. Question After the lengthy preamble, comes the inevitable question. Which method (do you think) is best?
Explanation of terminal objects using limits I understand terminal objects, but I falter when trying to achieve them using the approach of diagrams, the category of constant diagrams $\mathcal{C}^{\mathcal{J}}$ and limits.  I have the empty category 0, now I have a diagram $D: \mathcal{J} \to \mathcal{C}$. However this diagrams image should be empty, because there are no objects or morphisms to map. At this point, I'm confused, because I can't get to the universal property $\forall c \in \mathcal{C}. \exists! f: c \to T$ anymore, because I'm missing T. (EDIT: I get that this is how it is supposed to be)  But continuing with the category of constant diagrams, even if I have a constant diagram mapping everything to one object / morphism, there is nothing to map, so the functors image is still empty. So now I'm missing both $T$, and any kind of $c$.  EDIT: It seems to me, have I chosen 1, instead of 0, I would have come up with that is considered the definition of the terminal category. What am I missing? Proof that a sum of Bernoulli rvs has Binomial distribution Let $\{X_n, n \geq 1\}$ be iid Bernoulli random variables with $\Pr(X_1 = 1) = p = 1 - \Pr(X_1 = 0)$ and let $S_n = \sum\limits_{i=1}^n X_i$ be the number of successes in $n$ trials. Show $S_n$ has a binomial distribution by the following methods:  1.) Prove for $n\geq0,$ $1\leq{k}\leq{n+1}$  $$\Pr(S_{n+1} = k) = p\Pr(S_n = k - 1) + q\Pr(S_n = k)\text{ where }q = 1-p.$$  2.) Solve the recursion using generating functions.  For the first method, I think I need to use convolution of $n + 1$ Bernoulli rvs, but I cannot immediately see the progression. For the second method, I don't exactly understand how to proceed either. This is problem 1.2 from Resnick's Adventures in Stochastic Processes. How to derive variance-covariance matrix of coefficients in linear regression Derivative of $f(x)=|x|$ Is it possible to 'fly to location' in Leaflet in the same way as  example? Encopresis, I'm at my wits end? Does anybody have suggestions? My 12 year old daughter has been dealing with retentive encopresis since age 6. She avoids going to the toilet at all. This results in soiling and the production of an amazing huge toilet-clogging log-like object every week. We have been trying for years to get her to talk about this, but when we do talk about this, the response is ALWAYS "I don't know" despite what her pedi, GI doc and psychologist tried to do or suggested we do to get her to talk about it. We switched psychologists last year after a year of not talking about this, no luck.  We have tried so many treatment plans since diagnosis, specifically tailored to her, such as enemas, scheduled toilet sittings, rewards, consequences, psychologist, etc. she just keeps holding it in. Right before summer, her pedi and the GI doc laid out a clear treatment plan of laxatives, regular bathroom visits, and a rewards program. She did take the Miralax (2 table spoons) but she just kept holding it in and soiling her pants. She avoided going to the toilet. I made her stay at the toilet and told her "if you poop, I will buy you XYZ" (something she REALLY wants)" you can do XYZ only after you poop" and not letting her do things "you can't do XYZ until you poop" and XYZ would be something she LOVES to do. I wouldn't let her leave the toilet until she pooped, so she sat there for hours, no luck. When school started, she refuses to take the Miralax.  Now we laid out a new treatment plan. We TRIED to involve her, the response is again "I don't know." She is supposed to take an enema every morning and sit on the toilet, but she fights daily about taking it. When she does take it, she holds it in and won't sit on the toilet. There is also a diet plan she refuses to follow. She is not supposed to drink dairy, but drinks milk at school. Now the past 2 weeks she refused all treatment which we fight daily about. She refuses to sit on the toilet, no matter what we try. 2 years ago we tried ucanpooptoo, didn't help.  We asked her why she holds it in. Does pooping hurt? Does it scare you to poop? Does it feel good to hold it in? The answer is always "I don't know" and you can question her for hours and hear "I don't know". We explained to her digestion and she knows why she needs to poop. We ruled out any physiological reasons for retention. Luckily her friends don't notice, but one day her friends might smell her soiling, and I talked to her about this, how it would affect, boys , high school, etc. Her response was "I don't know" and she would not talk about this.  Does anybody have suggestions how to get her to start pooping in the toilet regularly? What is the best way to get security token of a API Only user? API only users cannot login and choose 'Reset my security token'. Right now, in my company, we select a System Administrator profile for users, log in for the first time, choose 'Reset my security token', then change the profile to a custom profile which has API only enabled.  The other way of doing it is, modifying the profile to not have API only user checked initially and then turn it on once the security token is received.  Is there a recommend way to do this? Is there an option to force the security token to be sent  when the user is created with a profile which has API user enabled? Replace strings in a file based on a list of strings and a list of corresponding replacements Live USB data gets lost on Restart I am crawling some data using Apache Nutch and storing it on my USB Drive. The OS itself is Ubuntu Live Boot OS. But for every restart or shutdown I am losing my data. I also tried suspending my Ubuntu user still data was not visible when I reinserted my live USB.  Is there any way I can keep the data intact and does not loose and application settings installations when I do restart Ubuntu.  Used USB installer provided by pendrivelinux.com at Please use UK pre-uni methods only (at least at first). Thank you. What happens when you plug two sides of a cable to a single networking device? What is likely to happen when you plug two ends of a network cable to a single switch/router? Will this create problems on the network, or just be ignored? I love R. Courant and F. John's Introduction to Calculus and Analysis because of its wide coverage, precise description and friendly written style.  Are there any classic textbooks like it on ODE/PDE?  thanks. How to prove those "curious identities"? How to prove $$ \prod_{k=1}^{n-1} \sin\left(\frac{k\pi}{n}\right) = \frac{n}{2^{n-1}}$$ and $$ \prod_{k=1}^{n-1} \cos\left(\frac{k\pi}{n}\right) = \frac{\sin(\pi n/2)}{2^{n-1}}$$ Badge overflow on users with too many badges When a user has &quot;too much&quot; reputation and &quot;too many&quot; badges, the design of the user card breaks:    The badge counters overflowing to another line. Can anything be done about this? For example, expanding the rep and badges to their own lines:  NameRepBadges I remember after Stargate: Universe was cancelled, that I read, somewhere on the web, either part of an interview or an article by one of the producers about what they were planning if there had been a third season.  (He said they weren't sure whether they'd set it soon after the ending of the second season, or later, or were going to do something else.)  He also wrote that, while he felt okay discussing that, he did not feel like he could reveal how the story about the superstructure the Destiny had found would turn out.  He said that was something the other two producers had worked out and he didn't want to reveal it and would leave it up to them to reveal it if they wanted to.  Was this ever revealed?  Did the other producers ever discuss or reveal anything about what was going to happen with that storyline?    Addition: I'm referring to the superstructure that is the message found in the background radiation throughout the universe, not anything from the planet builders or other easily seen physical structures. New installation – TeX editor says: "Error: Could not start the command" I just downloaded and installed Texmaker. I've never used any (La)TeX before.  When I click the Run button with an arrow next to "Quick build" I get an error saying "Could not start the command". Why? What do I need to do? Windows 7 USB install error I am trying to install Windows 7 Professional x64 from a bootable USB stick. I have used the Windows 7 USB DVD Download Tool to create the media from an ISO.  The machine boots from USB fine, and enters the Windows installation wizard but then I get an error stating:     A required CD/DVD drive device driver is missing. If you have a driver floppy disk, CD, DVD, or USB flash drive, please insert it now.   I have tried setting the SATA controller in the BIOS to both AHCI and ATA but neither appear make any difference.  I've also tried both the USB 3 and USB 2 ports with no difference.  I've also tried to use the files from an official Dell recovery CD as this is a Dell laptop but again, no dice.  All this because there's no optical drive in the machine!  Dell Vostro V131 / i5-2450M / BIOS A04  Can anyone offer any advice please? How much money did Harry Potter have? So, we know Harry . By descriptions from the books, it looks like he had enough money to spend during his school years with plenty to spare for later life.  Is there an estimate on how much money he had? How much would that be in muggle money? Was he extremely rich?  I'm thinking here that he could have received more than just an inheritance. For example, anonymous supporters could have given him money through the bank while he was a baby, or when he was about to go to Hogwarts, and he wouldn't have known (not even Dumbledore would have known). Can't move files to trash without any permission issues This is not like one of the cases addressed before - I've checked them all.  I've suddenly begun getting that message "... can't be put in the trash. Do you want to delete it immediately?" this morning in my Home directory only.  I have a Home partition (ext4, mounted properly) with symlinks for user folders like Downloads, Documents, etc. to folders with the same names in a Data partition (ntfs, mounted properly).  For example, I cannot move a file in Downloads folder to Trash when I try via Home, but no problem if I try via Data partition, although the file in question has the same ownership and permissions at both locations.  Also no problem at all with files in my Home folder, including sub-folders like Public that are not symlinked to my Data partition.  Interesting puzzle for me...  PS: Just upgraded to 15.04 but I don't think it matters... On second thought, it might perhaps be something to do with upgrading from Nautilus 3.10 to 3.14?  PPS: This bug has been finally resolved some time later in version 16.10, and no longer exists under Ubuntu 17.04. How to open a file manager of the current directory in the terminal? When I work in terminal, sometimes I want to open the current directory in a GUI file manager. And then to click the items in the window to run the application. How can I do this?