This question already has answers here:
Python: access class property from string [duplicate]
(4 answers)
Closed 4 years ago.
I am using Python 3.5 and wish to do something like this
I have created a class which has variables Bitcoin, Monero , 'Etherum' ,etc with various integer values ,I wish to extract them
var1="Bitcoin"
value=classobj.var1 // there is a class which has a variable called Bitcoin and its value is 10 I wish to get its value using classobject.Bitcoin but the variable called var is Dynamic
print (value)
How do I achieve the same ?
EDIT
I know it is possible using switch statement but I am looking for other ways
This is almost always a bad idea—and you really should explain why your design looks like this, because it's probably a bad design.
But "almost always" isn't "always", so Python has a way to do this:
getattr(classobj, var)
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I was wondering if there is a way that you can make a class instance by using concatenation.
(setting a class name like person1, person2, etc... , automatically)
I tried to make a code like:
class ID:
def __init__(self):
self.price = 2000
for i in range(50):
string = person + str(i)
string = ID()
But for some reason it didn't work.
Why can't this be defined this way?
Is this even possible?
usually people use a dictionary for this
people = {}
for i in range(50):
people["person"+str(i)] = ID()
if you are really determined to make a global variable you certainly can... but its a much better idea to use a dictionary
if you really wanted a variable named person1..50 though you could just do as follows
for i in range(50):
globals()['person'+str(i)] = ID()
print(person2)
but your ide and anyone who has to work with your code in the future will not appreciate it
This question already has answers here:
How to overload __init__ method based on argument type?
(10 answers)
Python function overloading
(19 answers)
Closed 4 years ago.
I am trying to build a complex number calculator in python. The number will be represented by a class with two fields. The first constructor I want to use looks like this:
def __init__(self,real,imag):
self.real = real
self.imag = imag
but i would like to use another, where the only parameter will be a string representing the whole number:
def __init__(self,string_number):
How to implement the overload that allows to use different number of parameters? I am more used to C++, where such thing was easy.
Thanks a lot!
EDIT:
Question marked as duplicate: I did not find any answer regarding different number of parameters. The error I get when trying to build a new instance of the class is about to few arguments.
This question already has answers here:
Why does code like `str = str(...)` cause a TypeError, but only the second time?
(20 answers)
Closed 6 years ago.
I have used print to store value like:
print=3
After then I am not able to use it to print any message:
print('a message')
Its giving error:
'int object is not callable'
Is there any way to use print both as a variable and a functions? If not then Why not python just makes built-in function names as keyword to remove this conflict?
Functions and data share the same namespace in Python -- as they do in many other languages (the entire family of LISP-1s comes to mind first, including Scheme and Clojure; also Ruby, Groovy, and I'm sure many more).
Thus no, you cannot do this. Widely available checkers (pylint, pychecker, etc) will catch and report on attempts to shadow builtins (such as print) with data.
This question already has answers here:
How can I represent an 'Enum' in Python?
(43 answers)
How do I create a constant in Python?
(44 answers)
Closed 6 years ago.
This question was marked as duplicate of another question. I am not asking how to create enumerations, I'm asking how to create constants, and that is the difference between the questions.
This question was marked as duplicate of yet another question. While the answers do explain how to create constants in python they don't explain how to create them in Enum like style, which is what my question is asking for.
I need to create constants in my python code. I don't want the user to be able to modify them. I found this, but it is not exactly what I need. I need to be be able to use them just like enums, without creating and instance.
For example:
class MyConstClass(object):
Const0 = 0
Const1 = 1
Const2 = 2
myfunc(MyConstClass.Const0)
MyConstClass.Const1 = 20 # Raise an exception
I also don't want to create a module, just like it is done here
Is there a way to do it?
This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 7 years ago.
I'd like to be able to access the name of a variable that is passed to a function as e.g.
def f(x):
'''Returns name of list/dict variable passed to f'''
return magic(x)
>>a = [1,2,3]
>>print( f(a) )
'a'
>>print( f(2) )
None
I suspect this is possible using Python introspection but I don't know enough to understand the inspect module. (NB I know that the need for a function like magic() is questionable, but it is what I want.)
Actually, this is not possible. In Python names and values are quite separate. Inside f, you have access to the value of x, but it is not possible to find out what other names other functions might have given to that value.
Think of names as being somewhat like pointers in C. If you have a pointer, you can find out what object it points to. But if you have the object, you cannot find out what pointers are pointing to it.