|
List Info
Thread: python instances and type
|
|
| python instances and type |

|
2007-04-16 03:27:35 |
Hello,
Sorry for the long email, but thanks in advance.
I am not quite sure what is happening, so I have not been
able to
adequately seek a solution via Google, so if this answer is
fairly
easy to find on Google, let me know. I am not sure how to
produce an
example of my problem without using the python code and
classes that I
have, so I will try to compose an effective theoretical
example
below. If there are any clarifications to be made please
let me know.
Basically the problem I am having is obtaining the proper
type
identification from a Python object. So going through the
motions,
lets say I have 2 types:
class foo_1:
data = ""
class foo_2:
foo_1s={}
Time goes by and some assignments are performed and foo_2 is
populated
with some foo_1 objects. Now, I am using the the function
below,
get_class_resolution to get the type of foo_1 and foo_2, and
it works
well when I don't perform any black magic voodoo and simply
use after
the object is created.
It would return the string "foo_1" for the example
below:
x = foo_1()
x.data = "boring"
print type(x), type(x).mro()
<class 'foo_1'> [<class 'foo_1'>]
print get_class_resolution(x)
foo_1
But I need to do black magic voodoo, and when I perform an
operation
similar to below
on a foo_2 object the get_class_resolution returns
"instance". In
fact I am expecting it to return foo_1.
foo_2o = foo_2()
foo_2o.foo_1s["boring"] = x
bar = foo_2o.foo_1s.values()[0]
print type(bar), type(bar).mro()
<type 'instance'> [<type 'instance'>, <type
'object'>]
print get_class_resolution(bar)
instance
I am not sure what the difference between the instance vs.
a class,
but I never thought the type of the object was allowed to
change. Any
help would be appreciated.
Thanks, Adam
def get_class_resolution(obj):
'''
get the first class resolution string for a
particular object
type obj: Mixed/ Any
param obj: this is the object to get the name of.
Good
for aiding in the comparison of two
objects
by name and heirarchy
rtype: string
return: class name resolution of the object
'''
# typical output is "[<class
'class.resolution'> <type 'object'>]"
print str(type(obj).mro())
print str(type(obj))
name = str(type(obj).mro()).split("'")[1]
if len(name.split(".")) > 1:
return
".".join(name.split(".")[:-1])
return name
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: python instances and type |

|
2007-04-16 03:50:54 |
Sorry, I cannot replicate your described behaviour:
andreas andi-lap:~> cat /tmp/t.py
def get_class_resolution(obj):
'''
get the first class resolution string for a
particular object
type obj: Mixed/ Any
param obj: this is the object to get the name of.
Good
for aiding in the comparison of two
objects
by name and heirarchy
rtype: string
return: class name resolution of the object
'''
# typical output is "[<class
'class.resolution'> <type 'object'>]"
print str(type(obj).mro())
print str(type(obj))
name = str(type(obj).mro()).split("'")[1]
if len(name.split(".")) > 1:
return
".".join(name.split(".")[:-1])
return name
class foo_1:
data = ""
class foo_2:
foo_1s={}
x = foo_1()
x.data = "boring"
print type(x), type(x).mro()
print get_class_resolution(x)
foo_1
foo_2o = foo_2()
foo_2o.foo_1s["boring"] = x
bar = foo_2o.foo_1s.values()[0]
print type(bar), type(bar).mro()
print get_class_resolution(bar)
andreas andi-lap:~> python2.4 /tmp/t.py
<type 'instance'> [<type 'instance'>, <type
'object'>]
[<type 'instance'>, <type 'object'>]
<type 'instance'>
instance
<type 'instance'> [<type 'instance'>, <type
'object'>]
[<type 'instance'>, <type 'object'>]
<type 'instance'>
instance
andreas andi-lap:~> exit
Amdreas
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: python instances and type |

|
2007-04-16 03:58:49 |
2007/4/16, Adam Pridgen <atpridgen mail.utexas.edu>:
> Hello,
>
> Sorry for the long email, but thanks in advance.
>
> I am not quite sure what is happening, so I have not
been able to
> adequately seek a solution via Google, so if this
answer is fairly
> easy to find on Google, let me know. I am not sure how
to produce an
> example of my problem without using the python code and
classes that I
> have, so I will try to compose an effective
theoretical example
> below. If there are any clarifications to be made
please let me know.
>
> Basically the problem I am having is obtaining the
proper type
> identification from a Python object. So going through
the motions,
> lets say I have 2 types:
> class foo_1:
> data = ""
> class foo_2:
> foo_1s={}
>
> Time goes by and some assignments are performed and
foo_2 is populated
> with some foo_1 objects. Now, I am using the the
function below,
> get_class_resolution to get the type of foo_1 and
foo_2, and it works
> well when I don't perform any black magic voodoo and
simply use after
> the object is created.
>
> It would return the string "foo_1" for the
example below:
>
> x = foo_1()
> x.data = "boring"
> print type(x), type(x).mro()
> <class 'foo_1'> [<class 'foo_1'>]
> print get_class_resolution(x)
> foo_1
Would it? When I try it out, I get:
>>> class foo_1:
data = ""
>>> x = foo_1()
>>> x.data = "boring"
>>> print type(x), type(x).mro()
<type 'instance'> [<type 'instance'>, <type
'object'>]
>>> get_class_resolution(x)
[<type 'instance'>, <type 'object'>]
<type 'instance'>
'instance'
To get your desired behaviour, you need something like:
>>> class foo_1(object):
data = ""
>>> x = foo_1()
>>> x.data = "boring"
>>> print type(x), type(x).mro()
<class '__main__.foo_1'> [<class
'__main__.foo_1'>, <type 'object'>]
>>> get_class_resolution(x)
[<class '__main__.foo_1'>, <type 'object'>]
<class '__main__.foo_1'>
'__main__'
>>>
--
Andre Engels, andreengels gmail.com
ICQ: 6260644 -- Skype: a_engels
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: python instances and type |
  United States |
2007-04-16 05:27:33 |
Adam Pridgen wrote:
> x = foo_1()
> x.data = "boring"
> print type(x), type(x).mro()
> <class 'foo_1'> [<class 'foo_1'>]
> print type(bar), type(bar).mro()
> <type 'instance'> [<type 'instance'>,
<type 'object'>]
As Andre has pointed out, this is a difference between new-
and
old-style classes, not an artifact of the way you are using
the classes.
The type and the class of an instance of an old-style class
are not the
same. To find out the class of an old-style class instance,
you should
use the __class__ attribute:
In [1]: class foo_1: pass # old-style class
...:
In [2]: class foo_2(object): pass # new-style class
...:
In [3]: f1 = foo_1()
In [4]: type(f1)
Out[4]: <type 'instance'>
In [5]: f1.__class__
Out[5]: <class __main__.foo_1 at 0x1201090>
For instances of new-style classes, it's class and type are
the same and
__class__ and type() give the same result:
In [14]: f2=foo_2()
In [15]: f2.__class__
Out[15]: <class '__main__.foo_2'>
In [16]: type(f2)
Out[16]: <class '__main__.foo_2'>
If you want to know the base classes that will be searched
for attribute
access of an old-style class, use inspect.getmro(). This
will work for
new-style classes also.
In [19]: import inspect
In [22]: class foo_3(foo_1): pass
....:
In [23]: f3=foo_3()
In [24]: type(f3).mro()
Out[24]: [<type 'instance'>, <type 'object'>]
In [25]: inspect.getmro(f3.__class__)
Out[25]: (<class __main__.foo_3 at 0x1272c90>,
<class __main__.foo_1 at
0x1201090>)
Kent
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: python instances and type |
  United States |
2007-04-16 05:31:07 |
Kent Johnson wrote:
> The type and the class of an instance of an old-style
class are not the
> same.
For a little more explanation, see
http://docs.py
thon.org/ref/node33.html
For a lot more explanation, see
http://www.py
thon.org/doc/newstyle.html
Kent
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
[1-5]
|
|