The following code from  to calculate the maximum path sum in Binary Tree.  In the function findMaxSum() they declared a variable as findMaxUtil.res What does that means ?   I saw  If function.variable inside a function it means that the function is kind of an object.But in this example the function name and variable are out of the original function. Would someone explain that please with a clear example!  class Node:      def __init__(self, data):          self.data = data          self.left = None         self.right = None  def findMaxUtil(root):      if root is None:          return 0       l = findMaxUtil(root.left)      r = findMaxUtil(root.right)       max_single = max(max(l, r) + root.data, root.data)      max_top = max(max_single, l+r+ root.data)      findMaxUtil.res = max(findMaxUtil.res, max_top)        return max_single   def findMaxSum(root):       findMaxUtil.res = float("-inf")  ## This line     findMaxUtil(root)      return findMaxUtil.res           ## and this line
2689
What is the idiomatic Python equivalent of this C/C++ code?  void foo() {     static int counter = 0;     counter++;     printf("counter is %d\n", counter); }   specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
71027
The following code fails to compile stating  "A local variable named 'st' cannot be declared in this scope because it would give a different meaning to 'st', which is already used in a 'child' scope to denote something else":           var l = new List&lt;string&gt;();         l.Find(st =&gt; st.EndsWith("12"));         string st = "why this fails?";   I understand why this won't work:          string preParent = "";         {             string preParent = "Should fail cause we change the meaning";         }   When we do the following we get "CS0103: The name 'postParent' does not exist in the current context":          {             string postParent=string.Empty;         }         postParent = "Should this work?";   What I don't get is why is the compiler smart enough to see that postParent is not within scope, but won't let me define a new variable that has the same name as a variable used within a child scope (which is obviously out of scope at this point).   Is the compiler simple enforcing scope by refusing to let me use the variable? If so this makes sense.  ===========  Edited:   I guess what I also find interesting is how you can have the same variable within two child scopes in a single method, so this is valid:          {             string thisWorks= string.Empty;         }         {             string thisWorks= "Should this work?";         }   I'm just a little curious that you can have two variables with the same name as long as they are at the same level (if you look at scope as a tree). This makes sense because you can have local variables in two methods of the same class with the same name.   I'm just surprised that the compiler is able to differentiate and allow this, while it wouldn't allow the postParent variable. And is this a technical limitation or was this a design decision? That's what I'm really trying to get at;-)
19743
I know that it is defined such that there is no more than one y-value output for any given x-value input, but I"m wondering WHY it is defined that way? Why can't we apply everything we know about functions to equations that have more than one y-value output for any given x-value input?   In short, what is the benefit or reason to define a function this way?  At the same time, a little historical context would also be appreciated (i.e., when the concept originated and why).  Thanks,  Moshe
92031
Whats this syntax useful for :       function(String... args)   Is this same as writing       function(String[] args)    with difference only while invoking this method or is there any other feature involved with it ?
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
13479
Note that this function does not have a "{" and "}" body.  Just a try/catch block:  void func( void ) try {     ... } catch(...) {     ... }   Is this intentionally part of C++, or is this a g++ extension?  Is there any purpose to this other than bypass 1 level of {}?  I'd never heard of this until I ran into
2152
What exactly are the Python scoping rules?  If I have some code:  code1 class Foo:    code2    def spam.....       code3       for code4..:        code5        x()   Where is x found?  Some possible choices include the list below:   In the enclosing source file In the class namespace In the function definition In the for loop index variable Inside the for loop   Also there is the context during execution, when the function spam is passed somewhere else. And maybe  pass a bit differently?  There must be a simple reference or algorithm somewhere.  It's a confusing world for intermediate Python programmers.
4021
What is the best way to go about calling a function given a string with the function's name in a Python program.  For example, let's say that I have a module foo, and I have a string whose content is "bar". What is the best way to call foo.bar()?  I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.
17203
I need to know if a variable in Python is a string or a dict.  Is there anything wrong with the following code?  if type(x) == type(str()):     do_something_with_a_string(x) elif type(x) == type(dict()):     do_somethting_with_a_dict(x) else:     raise ValueError   Update: I accepted avisser's answer (though I will change my mind if someone explains why isinstance is preferred over type(x) is).  But thanks to nakedfanatic for reminding me that it's often cleaner to use a dict (as a case statement) than an if/elif/else series.  Let me elaborate on my use case.  If a variable is a string, I need to put it in a list.  If it's a dict, I need a list of the unique values.  Here's what I came up with:  def value_list(x):     cases = {str: lambda t: [t],              dict: lambda t: list(set(t.values()))}     try:         return cases[type(x)](x)     except KeyError:         return None   If isinstance is preferred, how would you write this value_list() function?
123679
Two function definitions, the only difference is that the first combines the local storage keyword with the assignment, while the second separates them:  function foo {     local fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  function bar {     local fn     fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  foo bar   This echoes "0" then "1". I expect it to echo "1" then "1". It seems like the value of $? is the result of the assignment to local, rather than the result of the command substitution.  Why does bash 4.2.46(1)-release behave this way?
106
I need a good explanation (references are a plus) on Python's slice notation.   To me, this notation needs a bit of picking up.   It looks extremely powerful, but I haven't quite got my head around it.
1072
Is this defined by the language? Is there a defined maximum? Is it different in different browsers?
6080
I'm new to both JavaScript and . In YUI library examples, you can find many uses of this construct:  (function() {     var Dom = YAHOO.util.Dom,     Event = YAHOO.util.Event,     layout = null,         ... })();   I think the last couple of parentheses are to execute the function just after the declaration.  ... But what about the previous set of parentheses surrounding the function declaration?  I think it is a matter of scope; that's to hide inside variables to outside functions and possibly global objects. Is it? More generally, what are the mechanics of those parentheses?
48831
Is there a name for the function $ \max(x, 0) $?  For comparison, the function $ \max(x, -x) $ is known as the absolute value (or modulus) of x, and has its own notation $ |x| $
84261
Good afternoon,  I'm trying to learn lambda calculus, and I do understand the notation (it's not hard, $f=\lambda a_1.\cdots\lambda a_n.x=\lambda a_1\cdots a_n.x\Leftrightarrow f(a_1;\cdots;a_n)=x$), but not what is defined by it. For an example of something I don't understand, take the Church numerals:  $$ 0=\lambda f.\lambda x.x\\ 1=\lambda fx.f\ x\\ 2=\lambda fx.f(f(x))\\ \cdots\\ n=\lambda nfx.f^n\ x $$ (where $f^n$ is $f$ composed with itself $n$ times)  I see $0$'s definition is to not apply a function (apply it zero times), and $1$'s to apply it once, and $n$'s to apply it $n$ times. This kind of makes sense; however, what I don't get is how, formally, can a function be turned into a number. Shouldn't $\lambda f.\lambda x\ x$ be a function with two arguments, one of which is a function? Is it a function or is it a number? Wouldn't one have to pass values of to $f$ and $x$ to it for it to be a value? Is it a representation of the number? How does that really work?  Also, with true ($\lambda xy.x$) and false's ($\lambda xy.y$) definitions? How are those boolean values? They seem two argument functions that yield the first and second argument, respectively. Again, are they values or are they functions? Please help me out here, it's just so confusing to me. I've   like , but I'm still clueless. I know some programming, so any working program showing this would be most welcome.
82341
example:  a_list = [1, 2, 3] a_list.len() # doesn't work len(a_list) # works   Python being (very) object oriented, I don't understand why the 'len' function isn't inherited by the object.  Plus I keep trying the wrong solution since it appears as the logical one to me