variable-assignment

Why can't I assign python's print to a variable?

[亡魂溺海] 提交于 2019-12-04 04:29:55
I'm learning to program and I am using Python to start. In there, I see that I can do something like this: >>>> def myFunction(): return 1 >>>> test = myFunction >>>> test() 1 However, if I try and do the same with print it fails: >>>> test2 = print File "<stdin>", line 1 test2 = print ^ SyntaxError: invalid syntax Why is print different than a function I create? This is using Python v2.7.5. BrenBarn print is a statement , not a function. This was changed in Python 3 partly to allow you to do things like this. In Python 2.7 you can get print as a function by doing from __future__ import print

matlab conditioned matrix assignment

梦想的初衷 提交于 2019-12-04 03:51:24
问题 i have a question about matrix assignment. say i have three matrices A, B and C, and i want to assign the elements of matrix C to the elements of A and B according to the rule C[i,j] = A[i,j] if abs(C[i,j] - A[i,j]) < abs(C[i,j] - B[i,j]) C[i,j] = B[i,j] if abs(C[i,j] - A[i,j]) > abs(C[i,j] - B[i,j]) C[i,j] = 0 if abs(C[i,j] - A[i,j]) == abs(C[i,j] - B[i,j]) how can i write it without for loops? thanks very much for your help. 回答1: I think Dan Becker has the right idea, but re-computing abs(C

NameError: undefined - have parsing rules for local variables changed in Ruby 2.1.2?

元气小坏坏 提交于 2019-12-04 03:20:30
问题 I am getting NameError: undefined local variable or method with ruby 2.1.2 As observed in this question, expressions like: bar if bar = true raises an undefined local variable error (provided that bar is not defined prior) because bar is read by the parser before it is assigned. And I believe that there used to be no difference with that with this expression: bar if bar = false The difference between the two is whether the main body is evaluated or not, but that should not matter if

Assignment in R language

六眼飞鱼酱① 提交于 2019-12-03 23:54:50
I am wondering how assignment works in the R language. Consider the following R shell session: > x <- c(5, 6, 7) > x[1] <- 10 > x [1] 10 6 7 > which I totally understand. The vector (5, 6, 7) is created and bound to the symbol 'x'. Later, 'x' is rebound to the new vector (10, 6, 7) because vectors are immutable data structures. But what happens here: > c(4, 5, 6)[1] <- 10 Error in c(4, 5, 6)[1] <- 10 : target of assignment expands to non-language object > or here: > f <- function() c(4, 5, 6) > f()[1] <- 10 Error in f()[1] <- 10 : invalid (NULL) left side of assignment > It seems to me that

println in scala for-comprehension

你。 提交于 2019-12-03 22:08:34
In a for-comprehension, I can't just put a print statement: def prod (m: Int) = { for (a <- 2 to m/(2*3); print (a + " "); b <- (a+1) to m/a; c = (a*b) if (c < m)) yield c } but I can circumvent it easily with a dummy assignment: def prod (m: Int) = { for (a <- 2 to m/(2*3); dummy = print (a + " "); b <- (a+1) to m/a; c = (a*b) if (c < m)) yield c } Being a side effect, and only used (so far) in code under development, is there a better ad hoc solution? Is there a serious problem why I shouldn't use it, beside being a side effect? update showing the real code, where adapting one solution is

Assignment with “or” in python

廉价感情. 提交于 2019-12-03 22:08:17
Is it considered bad style to assign values to variables like this? x = "foobar" or None y = some_variable or None In the above example, x gets the value 'foobar'. No, it's a common practice. It's only considered bad style for expressions that are considerably longer than yours. The primary danger of doing something like this is the possibility that (in the second case) some_variable is False but not None (the integer 0 , for instance) and you don't want to end up with y equal to None in that case. I also feel a bit unconfortable using that kind of expressions. In Learning Python 4ed it is

Why does i = i + i give me 0?

我与影子孤独终老i 提交于 2019-12-03 18:22:08
问题 I have a simple program: public class Mathz { static int i = 1; public static void main(String[] args) { while (true){ i = i + i; System.out.println(i); } } } When I run this program, all I see is 0 for i in my output. I would have expected the first time round we would have i = 1 + 1 , followed by i = 2 + 2 , followed by i = 4 + 4 etc. Is this due to the fact that as soon as we try to re-declare i on the left hand-side, its value gets reset to 0 ? If anyone can point me into the finer

Erlang list comprehension

我们两清 提交于 2019-12-03 16:41:09
I'm testing an expression with two inequalities for the condition of a list comprehension. Is there a way to have assignments here and not duplicate that expression? (The following code doesn't work, but I wish it would) diagnose(Expertise,PatientSymptoms)-> {[CertainDisease|| {CertainDisease,KnownSymptoms}<-Expertise, C=length(PatientSymptoms)-length(PatientSymptoms--KnownSymptoms), C>=2, C<=5 ]}. A way of writing it directly without a fun would be to use a begin ... end block ending with a boolean test: [ CertainDisease || {CertainDisease,KnownSymptoms} <- Expertise, begin C = length

Use of Colon in object assignment destructuring Javascript

我们两清 提交于 2019-12-03 16:29:02
Working with React.js and React Router import React, { Component } from 'react'; const PrivateRoute = ({ component: Component, ...rest }) => ( <Route {...rest} render={} /> ) *{ component: Component, ...rest }* ..rest is the use of spread syntax but what does *component: Component* do Suman Kundu In ES6 this will assign the value to a new variable in this case named foo let obj = { name: 'Some Name', age: '42', gender: 'coder' }; let { name: foo, ...rest } = obj; console.log({foo, rest}) // { foo: 'Some Name', rest: { age: 42, gender: 'coder' } } // In this case, name will not be defined for

Is there a right way to return a new object instance by reference in C++?

空扰寡人 提交于 2019-12-03 15:37:46
问题 So I was writing some code, and I had something like this: class Box { private: float x, y, w, h; public: //... Rectangle & GetRect( void ) const { return Rectangle( x, y, w, h ); } }; Then later in some code: Rectangle rect = theBox.GetRect(); Which worked in my debug build, but in release there were "issues" returning that Rectangle by reference -- I basically got an uninitialized rectangle. The Rectangle class has an = operator and a copy constructor. Without getting into why this broke, I