I am confronted to a python problem. I want to use type()
to find out what type of variable I am using. The code looks similar to this one:
class Foo():
array=[ myField(23),myField(42),myField("foo"), myField("bar")]
def returnArr(self):
for i in self.array:
print type(i)
if __name__ == "__main__":
a=Foo()
a.returnArr()
Edit: myField() is a class I defined.
When I ask for the type() I get: <type 'instance'>
Now according to 1 this is due to the fact that I use a class element and ask for type()
on that. Now what I need is: <type 'int'>
for the example: myField(42)
and <type 'str'>
for myField(foo)
. How could I acheive that?
EDIT:
def __init__(self, name, default, fmt="H"):
self.name = name
if fmt[0] in "@=<>!":
self.fmt = fmt
else:
self.fmt = "!"+fmt
self.default = self.any2i(None,default)
self.sz = struct.calcsize(self.fmt)
self.owners = []
The code is taken from scapy and I try to adapt it.
type()
of what? your question is both unclear and ambiguous. – ExtemporemyField
andfoo
andbar
? Where are they initialized? Simply populating a list with ints and strings and then callingtype
on it's elements should work. – ConnotationmyField
is defined. And could you also explain whyfoo
inmyField(foo)
should be a string? Did you actually mean"foo"
? – NorthwardmyField
,foo
, andbar
, and it gave the expected results. There's something relevant Steve hasn't told us. – Cockleshell<class '__main__.myField'>
, which is exactly what I would expect. – Cockleshell