Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I was working with Python3, and trying some sorts of code. And then i came to try some function features, here is my code
def print_list_members(some_list):
for i in first_list:
print(i)
that's all my function definition. and then i add for example new to the code
first_list = ["Alfried", "Michael", "John"]
second_list = ["Joseph", "Tim", "Delta"]
then i try to produce traceback by passing different argument with the function code
print_list_members(second_list)
but, no traceback raised, except something make me a bit confused, the output is
Alfried
Michael
John
the question is, how it be possible? or is it an error from python itself?
Change your code here
def print_list_members(some_list):
for i in some_list:
print(i)
You iterate over the global first_list inside the body of the function, so you print first_list. Whatever you pass as an argument is ignored. Perhaps you wanted iterate over some_list?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm a novice python programmer and thinking this is a very simple task.
I'm trying to use a function argument as a value within a df variable when calling the function, but it is returning the argument address and not the argument value.
def func_name(var_name):
df['varname']=str(var_name)
func_name(split_rand)
df
I want varname to contain "split_rand" throughout, but it contains <function split_rand at 0x0000025E4EAD9A60>. I know that enclosing 'split_rand' in quotes will work, but I don't want to use that for alternative reasons.
Thank you
It's returning the string representation of that function name. If you want the actual function name, then you do func.__name__
I think you meant to do df['varname']=var_name.__name__ instead and I'd rename var_name to func.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
this is my first post.
The Problem:
I have 2-Dimensional-list which is decleared and filled in a function - looks like this: data = [[1, 2, 3, 4], [10, 20, 30, 40]] - Now I want to pass that list from this function to another to continue working with the list.
I think that you're wanting to do something like this:
def myfunction():
data = [[1,2,3,4],[10,20,30,40]]
return data
def function2(myList):
# do stuff with it here
myList = myfunction()
function2(myList)
But your question is not very clear.
I think it's not an issue with python. Python allows You to pass multidimension lists to functions.
There shouldn't be any issue with this.
just pass like a simple variable.
I think I know your problem, it is the same process to pass a multidimensional array to a function as any other variable, the problem is other code, python is really annoying with tabs and spaces and it can cause confusing errors aside from the usual inconsistent use of tabs and spaces error. Just try deleting the function that's not working and rewrite it and you'll probably fix your error
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Suppose I have a function -
def foo(x,y):
pass
a = foo(5,6)
How do I access the values 5 and 6 from a?
From the code you have shown us, you cannot -- 5 and 6 were passed in to foo, you didn't keep a copy of them, foo didn't keep a copy of them, so they are gone.
So, as the above paragraph hinted, somebody has to keep a copy of those arguments if you want to do something else with them later, and while it is possible to have a function do so, that's not really what they are intended for. So your easy options are:
make foo a class that saves the arguments it was called with (which is still highly unusual), or
save the arguments yourself (arg1, arg2 = 5, 6 for example)
You can't. You'd need to use an object.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone explain the reason why python behaves differently in the following two cases please? Much appreciation.
def modifyNone(x):
print("B4:"+str(x))
# x.append(5)
x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[]
Method:
def modifyNone(x):
print("B4:"+str(x))
x.append(5)
# x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[5]
Python is pass by value, so you'll have to reassign a returned value like so:
a = modifyNone(a) # where the function returns a value
You don't seem to understand variable scope as well, try the documentation.