I am using a builtin SNMP library to get some data off a server. Now when I get it back it shows up in a list as
.>>> [ObjectName(1.2.6.1.4.1.111.1.4.1.7), Integer(21)]
When I print out x[0]
(pretend x is that response^) I see 1.2.6.1.4.1.111.1.4.1.7 and 21 for x[1]
but when I do type(x[0])
I get <type 'instance'>
.
Now my question is that is there a way to find the type of x[1]
and convert it to that? ( x[0]
will always be a string but x[1]
can be integer, string or float etc.
Is there a way to read that "Integer" (in this case)?
Thanks
Edit:
I dont think I described it clearly. I really done care about x[0]
at all, I want to convert the x[1]
to what it is without modifying anything from the class that I am getting the SNMP data from. In this example its an int
but it can be a float or a string. I am looking for a way to determine what the type of that instance is and then convert it to that
You get <type 'instance'>
because the library is using old-style classes . You can get at the class name by using x.__class__
. In your example x[0].__class__.__name__
would be ObjectName
.
What an object is and what is shown when you print that object are not necessarily related.
Example:
>>> class Foo:
... def __str__(self):
... return 'my personal string'
>>>
>>> f = Foo()
>>> type(f)
<class '__main__.Foo'>
>>> print(f)
my personal string
So if you want to know the type of x[1]
you have to call type(x[1])
.
And to try to convert x[0]
to the same type, use:
x = [ObjectName(1.2.6.1.4.1.111.1.4.1.7), Integer(21)]
converted = type(x[1])(x[0])
Even though if it'll work depends on what x[0]
and x[1]
really are.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.