using variable value in other variable [closed] - python

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 4 years ago.
Improve this question
I have a variable such as:
x = 'HELLO'
I want to access a DIC using the following string
self.dname.x[0]
which inherenlty should mean
self.dname.HELLO[0]
what is the proper way to do this in python? What is the proper term for this type of variable?

I'm not sure to understand the question, but you might want to have a look at the getattr builtin function (https://docs.python.org/3/library/functions.html#getattr).
For example, in your case (again, if I understand correctly) :
x = 'HELLO'
getattr(self.dname, x)[0]

Assuming self.dname is a dictionary:
self.dname[x][0]
x is an "index" variable - you are using it to find the data you want. Here's a related question going the other way.
Example: (tested in Python 2 and 3)
>>> foo={'HELLO': [1,2,3]}
>>> foo
{'HELLO': [1, 2, 3]}
>>> foo.HELLO[0] <--- doesn't work
AttributeError: 'dict' object has no attribute 'HELLO'
>>> x='HELLO'
>>> foo[x][0] <--- works fine
1
You asked about self.x. If x is a regular variable, you don't reference it with self. (as in the example above). If x is a member of your class, then yes, you will need to use self.x. However, if you are carrying around a key and a dictionary, you might consider redesigning your data structures to carry the resulting value instead.

Related

How to pass a 2-dimensional list to a function? [closed]

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

How to update a variable inside a function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a question about two different ways of writing a piece of code. I want to know whether they are both okay or one is better under some conditions? Basically, is it better to give the variable we want to update to the function or not?
def f1(num):
output.append(num)
output = []
f1(2)
print(output)
and
def f1(num, output):
output.append(num)
output = []
f1(2, output)
print(output)
In the first example, your function works for only adding element to globally defined certain array. And it is not good approach, you cannot use it for another array.
Second one has generic approach which is better. But only one small correction; you have an array named output, and you pass it to your function, but you keep its name same in your function. So, for your function, there are two output one global and one local, better use different names in this case:
output = []
def f1(num, arr):
arr.append(num)
f1(2, output)
print(output)
Please see warning PyCharm shows in same naming case:
Consider avoiding to use the first example where possible: global variables can be very difficult to work with, generating problems you never find easily. Instead, use the second piece of code.
You could also write something like the following code:
output = []
def add(num, listName):
listName.append(num)
return listname
for _ in range(5):
output = add(_, output)
print(output)

Passing a string to eval in method [closed]

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.
Improve this question
Struggling with passing a variable reference to a nested function. Using a dictionary is not an option in my use case. It's a much simplified MRE (real use passes an object with many nested objects).
def func(reference):
eval('trueVal=' + reference)
print(trueVal) #Expecting trueVal=15000
trueValue = 15000
reference = 'trueValue'
func(reference)
eval evaluates expressions. The result of your expression in your example can then be assigned to trueVal explicitly:
trueVal = eval(reference)
I would not endorse using eval or exec, 99 times out of 100, there is a better way to do it, dictionary is not the only option but without posting your question its impossible to provide a better way to approach it. below is for reference as an example that works without hardcoding the variable name. But really there is always likely a better approach thatn eval or exec.
def func(reference, value):
exec(reference + '="' + str(value) +'"')
print(reference, ":", eval(reference)) #Expecting trueVal=15000
trueValue = 15000
reference = 'trueVal'
func(reference, trueValue)

Access value of function attribute in python [closed]

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.

What's the difference between object.function and object.function() in python3? [closed]

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 7 years ago.
Improve this question
Admission: in python, I sometimes forget to include the () when I'm calling a method, so I accidentally use object.method instead of object.method().
The error I get often appears to indicate that my object.method call has returned the method itself, instead of calling the method. I'm wondering if this means that by using object.method or perhaps just function you can pass functions/methods around as arguments?
Bonus: If so, what are some applications of this? I know, for example, that lambdas can be used to pass anonymous functions as parameters.
It returns a function object that you could use later. For example
def myPrint(fun):
for i in range(5):
print(fun(i))
def myFactorial(x):
acc = 1
for i in range(1,x):
acc *= i
return acc
Then you can do
>>> myPrint(myFactorial)
1
1
2
6
24
Or
>>> myPrint(range)
[]
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
This allows you to create a function that you want to use in other context (And it's pretty common in python), lot of built-in functions do this, like map, filter, reduce, sort...
As you can see myObject.myFunction returns the function itself while myObject.myFunction() actually "executes" it
It is used for so called callbacks. YOu can pass the function as argument and after you have done your magic in the function call the callback. Usefull if you do async stuff.
Yes. object.method returns the method as an object.

Categories