1

I am translating a program from perl to python and I really don't know how to translate a scalar variable to python. My problem is that I want to call the first value that comes in based on all the scalar variables used in the code, a scalar variable when mentioned it refers to the value assigned to it.

the specific area was

my $min = $_[0];
my $max = $_[1];

So these two variables were assigned a value before hand, now what I am trying to do is call my min and let it be the first value that comes, then I want my max to be called as the second value that comes in. Basically, trying to gather the info that was generated to min and max and now bring it to here. Do not understand how to do this in python.


The thing is that I have 3-5 different values assigned to min and also to max, I dont know how to bring in the value.

i am the one who put the question, but I logged in as i think a temporary user and it got deleted. Thanks!

2
  • 2
    Can you explain what you mean by "call my min"? Usually the word "call" refers to calling a subroutine or function; it doesn't make much sense to talk about "calling" a value. And note that $_[0] and $_[1] are the (first two) elements of @_, which is the array variable that holds the arguments to a function. In Python you'd just declare the parameters. Commented Aug 18, 2011 at 2:33
  • @mit - can you go to your account on Programmers: programmers.stackexchange.com/users/34534/mit and register the account. The associate it with the stack overflow one. That way we can marry up your SO account with this question. Thanks. Commented Aug 18, 2011 at 23:43

3 Answers 3

3

I'm not exactly sure what you are asking for, but just FYI Python does not attach types to the names in the script. The type is the object that the name refers to. So there is not distinction of "scalar" variable or "list context", or anything like that.

So it looks like you have a list. you can do:

mymin = mylist[0]
mymax = mylist[1]

Assuming mylist holds reference to a list object (in Python every name is a reference).

Sign up to request clarification or add additional context in comments.

3 Comments

Or you could call them "min" and "max" instead of "mymin" and "mymax".
@compman Those names are discouraged in Python since they are the same as, and would mask, the global functions min and max.
This is why Perl uses sigils in front of it's variables, so that you never need to worry about accidentally masking some built-in global.
3

Yes, I'm sure you're talking about subroutine args! In Python, you don't receive arguments in a single list '@_' as in Perl. instead, the simplest way to do this is:

def func(arg1, arg2):
   min = arg1
   max = arg2
   #do your stuff

This is roughly equivalent to:

sub func{
   my($min, $max) = @_;
   #your stuff
}

Now if you must receive your args as a list or as a tuple (both are collections), this should do:

def func(args):
    min, max = args[0], args[1]
    #your stuff

You can replace `args' with anything you like, staying within Python variable naming rules.

I recommend you read up on the book linked to in the previous answer and especially on functions, as Python has a plethora of possibilities when it comes to functions - positional and keyword arguments and the likes..

2 Comments

In your first code snippet, I'd just write def func(min, max):. It's functionally equivalent but stylistically better because the arguments have more descriptive names, and also you don't use unnecessary local variables.
Yes, you're right. Even I would do that :) I included that extra step there just to clarify the way Python handles function args, and that, which sounds really natural to us Pythonics, is pretty awkward in the beginning if you happen to come straight from Larry Wall's school. Thanks for the comment :)
1

I think you're talking about subroutine arguments. The big diff between perl and python with function calls (in python they are more like methods..) is that python can pass different things as a single argument, as a regular operation, and while perl can do this, it is rare. So if you are calling a 'function' in python you need to be aware of what the argument 'is'. Is it an 'array' (list or dict or tuple) or scalar variable or string or a byte. If it is a string or a byte then python 3.x introduces a lot of strict typing and syntax checking which will cause a lot of headaches initially; whereas perl converts between strings and bytes quite easily, python does not (because python wants to force data types for unicode strings).

The upshot is to reference just the name (no $name like in perl) and manually keep track of what the argument's type is:

arg1 = 50
arg2 = 70
myPythonFunc(arg1, arg2) 

....
def myPythonFunc(arg1, arg2):
    if (arg1 > arg2):
        return arg2
    ...

If you are really destined to use arrays (there's more advantages to using individual arguments because of default values and named args and such though), then:

args = [ 50, 70 ]
myPythonFunc(args) 

....
def myPythonFunc(args):
    if (args[0] > args[1]):
        return args[1]
    ...

Read this: http://diveintopython.net/native_data_types/lists.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.