Call by name/reference/value - programming-languages

Can someone explain call by name, reference, and value in depth and also compare them to each other?
Simple examples would be great as well. I am really focused on call by name, it feels like it's very similar to call by reference.

call by name : in call by name actual argument is not evaluated at the place of function calling rather they replace all the instance of corresponding formal parameters in text.
Actual argument are evaluated as many times as required.
Actual argument are evaluated within "caller" environment (if needed) :

Related

Python SCons Action?

I've been searching the documentation for a long time and still can't figure this out.
In the SCons documentation, there is discussion about adding a PreAction or PostAction. In the examples, they call an example method:
foo = Program('foo.c')
AddPreAction(foo, 'pre_action')
This calls 'pre_action' before 'foo.c' is compiled and linked. However, there is no explanation about what format 'pre_action' must take.
I have discovered that there are several pre-built actions, such as 'Copy'. However, I cannot find the source of 'Copy' to reverse-engineer it and write my own.
Can anyone point me to a guide as to how to create my own Actions? I either need an interface for calling my own method such as 'pre_action' above, or a guide for writing an Action class.
Really, even just some example code that is actually complete enough to use would be helpful...
The manpage section Action Objects lists the types of things that can be passed to the Action factory function to create an action; that is also what you pass to AddPostAction and AddPreAction as the second argument - that is, either an Action already made by a previous call to Action, or something that can be converted into one like a command string, or a list of such, or a function with appropriate signature. Pre/Post will simply call the Action function with that argument. So in that section, where there's an example with a call to Action, you could just plug that argument into AddPreAction, or you could save the result of calling Action and give that as the argument to AddPreAction.
The amount of flexibility here makes it a little tricky to document concisely.
(btw the source to Copy is a function called copy_func but you probably don't want to use that form because it's a couple of extra levels of abstraction you won't need)

Passing argument to lua function after evaluating function in string

I have functions process and matrix. The following code works
process(matrix({{2,4,6},{8,10,12},{14,16,20}}))
However the following doesn't work.
n='matrix({{2,4,6},{8,10,12},{14,16,20}})'
process(n)
It throws some error. The reason is obvious that process takes n as string rather than the output of the function matrix. So the basic difficulty involved here is about evaluating string from variable n and then give it as argument to the function process. Here loadstring function is of no use as matrix is local function and can't be referred from loadstring.
Is there any work around for this? I hope that I have clearly stated the problem here. It is about evaluating (or unloading) string and then passing it as argument to another function. Any help will be appreciated. Thanks.
as matrix is local function
Lua takes local declarations seriously. If a variable is declared local, it can only be accessed by code which is statically within the local scope of that variable. Strings which you later turn into code are not statically in the local scope and therefore cannot access local variables.
Now, with Lua 5.2+, you can provide load with a second parameter, a table which represents the global environment against which that Lua chunk will be built. If that table contains a matrix value, then the loaded string can access it. For Lua 5.1, you'd have to use setfenv on the function returned to load to accomplish a similar effect. The Lua 5.2+ method would look like this:
local env = {matrix = matrix}
local func = load("return matrix({{2,4,6},{8,10,12},{14,16,20}})", nil, "t", env)
process(func())
Note the following:
You must create an explicit table which is the global environment. There's nothing you can pass that says "make my locals available"; you have to put every local you'd like to access there. Which is, generally speaking, why you pass these things as parameters or just make them globals.
You explicitly need the "return " there if you want to get the results of the call to matrix.
You have to call the function. Functions are values in Lua, so you can freely pass them around. But if you want to pass the results of a function to another function, you have to actually call it.

Enter new variable as argument

I want to create a function that takes some chain of characters as an argument, and uses it as a str object.
def useless_function(argument) :
print(argument)
useless_function(banana)
--> NameError: name 'banana' is not defined
So this is what I did : I created a decorator that turns whatever I enter as argument into a str my function can print.
def decorator(f) :
def wrapper(arg_f) :
str_arg = str(arg)
f(str_arg)
return wrapper
So now I can decorate useless_function with my decorator, and useless_function(banana) will print 'banana'. And it will work with whatever it enter as an argument of useless_function.
My question is : is there a more elegant way or a simpler and faster way to do this automatic transformation into a string that can be used as an argument ?
Can you please elaborate because I don't understand what it is that you are looking for or saying.
If you mean: inside a function can you do input("variable")? Then the answer is yes. It is just essentially raw_input() from python2. The input from your keyboard will always be a str if I am not mistaken.
Update after edited post:
It is still not any more clear what you are trying to do.
At the end of the function, you do return * but I assume you know this.
I am really confused, but have you considered just doing str(argument)? As in takes_argument(str(argument))
2nd Update after 2nd edit:
I think I finally understand what you are trying to do, but I might be wrong.
Now, the problem is that def useless_function(argument) : will expect argument to be defined as a variable with some value(s). I am not aware of any other way than actually putting "argument" to tell python that what you are inserting is a string of characters rather than trying to reference some variable and its value. It is the same case as with print('something'), if I were to put print(something), python would try to look up the variable called something which you haven't defined.
Hope that makes sense.

Read statement in fortran

I am trying to understand a small read function in my program I am trying to decipher.The code is below
READ (LREST, END=350, ERR=350) ICHR
IF (ICHR .EQ. ICKLNK) THEN
DO L = 1, 4
READ (LREST, END=350, ERR=350)
ENDDO
So basically LREST is some kind of an argument provided for this subroutine this function is in. However, I found that LREST is not defined anywhere(used grep to see where LREST is defined in my *.f files. So my questions is what is that LREST doing there in READ function. I thought the location LREST is at is where the unit is defined.
Second questions is that ICHR is a 16 character string variable define for this subroutine. However, the contents of ICHR have not been assigned. I have no idea what this READ function is trying to read from.
Going over to IF statement, ICKLNK is another 16 character string variable with a defined strings. Because ICHR is not defined, does that mean this if statement never gets entered in?
Finally, the do loop( or for loop) has variable L in it but it is not even being used for read function inside of the loop.
Im a beginning in fortran so I may just be lacking a very basic knowledge but if you know an answer to my question please let me know. Thanks!
Han
You are correct that LREST is specifying the unit number (or internal file if it is character). You seem to suggest that LREST is an argument in this subroutine or function, which means its value is passed in by whoever calls the function. Showing us only a small piece of the code makes it hard to provide further details.
Again, you say ICHR is an argument to the procedure, so it takes on the value of whatever was passed in by the call. ICLNK is probably similar, but you didn't show all the code.
The DO (not for) loop is using L just as a counter; it doesn't need to be referenced inside the loop.

Lambda commands for tracing tkinter variables [duplicate]

Python has classes for Tkinter variables StringVar(), BooleanVar(), etc. These all share the methods get(), set(string), and trace(mode, callback). The callback function passed as the second argument to trace(mode, callback) is passed four arguments, self, n, m, x.
For an example of a BooleanVar() these appear to be '', 'PYVAR0', 'w'.
The third argument x appears to be the mode that triggered the trace, in my case the variable was changed. However, what is the first variable that appears to be an empty string? What is the second, if I had to guess I'd say some internal name for the variable?
The first argument is the internal variable name. You can use this name as an argument to the tkinter getvar and setvar methods. If you give your variable a name (eg: StringVar(name='foo')) this will be the given name, otherwise it will be a name generated for you by tkinter (eg: PYVAR0)
If the first argument represents a list variable (highly unlikely in tkinter), the second argument will be an index into that list. If it is a scalar variable, the second argument will be the empty string.
The third argument is the operation, useful if you are using the same method for reading, writing and/or deleting the variable. This argument tells you which operation triggered the callback. It will be one of "read", "write", or "unset".
Tkinter is a python wrapper around a tcl/tk interpreter. The definitive documentation for variable traces can be found here: http://tcl.tk/man/tcl8.5/TclCmd/trace.htm#M14. Though, this only documents how the internal trace works, the tkinter wrapper sometimes massages the data.
The first argument is the name of the variable, but is not "useless" since you can set it when you declare the variable, e.g.:
someVar = IntVar(name="Name of someVar")
When you check the first argument in the trace callback it will equal "Name of someVar". Using the name to distinguish between variables, you can then bind the same handler to trace changes to any number of variables, rather than needing a separate handler for each variable.

Resources