Scalar Variables from Perl to Python?
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 differe开发者_C百科nt 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!
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).
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..
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
精彩评论