471,330 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

listing the type of an object

How can I list a type of an object instance ?

I tried:

class tLED (tDevice):
def some_proc(self):
print 'type(self)', type(self)

But i gives me:
type(self) <type 'instance'>

Moreover, I want even the type to be listed by it's ancestor, like this

class tDevice:
def some_other_proc:
print 'type(self)', type(self)

thanks,
Stef Mientki

Jun 27 '07 #1
5 1014
Stef Mientki a écrit :
How can I list a type of an object instance ?

I tried:

class tLED (tDevice):
<ot>
Do yourself (and the world) a favour and give up hungarian notation...
This should be:

class Led(Device):
#...

</ot>
def some_proc(self):
print 'type(self)', type(self)

But i gives me:
type(self) <type 'instance'>
looks like Device is an old style class.
Moreover, I want even the type to be listed by it's ancestor, like this

class tDevice:
def some_other_proc:
print 'type(self)', type(self)
Try this:

class Device(object):
def some_other_proc(self):
print self, type(self)

class Led(Device):
pass

led = Led()
led.some_other_proc()
Jun 28 '07 #2
Bruno Desthuilliers wrote:
Stef Mientki a écrit :
>How can I list a type of an object instance ?

I tried:

class tLED (tDevice):

<ot>
Do yourself (and the world) a favour and give up hungarian notation...
This should be:

class Led(Device):
#...

</ot>
Didn't know that this was called "Hungarian notation",
and although it's not my personal favorite, it is thé standard in Delphi,
but I admit that it's not a very good choice in a object oriented
language like Python.

The program I'm writing now is might be an exception,
I'm writing a program that should be extended by non-Python-programmers,
even maybe non-programmers !
So I'm trying to use a simplest approach from the viewpoint of the end
users.
>
> def some_proc(self):
print 'type(self)', type(self)

But i gives me:
type(self) <type 'instance'>

looks like Device is an old style class.
>Moreover, I want even the type to be listed by it's ancestor, like this

class tDevice:
def some_other_proc:
print 'type(self)', type(self)

Try this:

class Device(object):
def some_other_proc(self):
print self, type(self)

class Led(Device):
pass

led = Led()
led.some_other_proc()
thanks Bruno,
that does the job.

cheer,
Stef Mientki
Jun 28 '07 #3
stef a écrit :
Bruno Desthuilliers wrote:
>Stef Mientki a écrit :
>>How can I list a type of an object instance ?

I tried:

class tLED (tDevice):

<ot>
Do yourself (and the world) a favour and give up hungarian notation...
This should be:

class Led(Device):
#...

</ot>
Didn't know that this was called "Hungarian notation",
and although it's not my personal favorite, it is thé standard in Delphi,
Yeps. I know. But Python is not Delphi.
but I admit that it's not a very good choice in a object oriented
language like Python.
MHO is that it's not a very good choice, period. But that's another
troll !-)
The program I'm writing now is might be an exception,
I'm writing a program that should be extended by non-Python-programmers,
even maybe non-programmers !
AFAICT, lots of "non-programmers" are using Python.
So I'm trying to use a simplest approach from the viewpoint of the end
users.
I think the simplest approach is to use the simplest possible naming
scheme. Which is to stick to the idiomatic naming conventions, most of
them described in pep08.

My 2 cents...
Jun 28 '07 #4
On 2007-06-28, Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ote:
Stef Mientki a écrit :
>How can I list a type of an object instance ?

I tried:

class tLED (tDevice):

<ot>
Do yourself (and the world) a favour and give up hungarian notation...
This should be:

class Led(Device):
#...
Using a naming convention for class objects, e.g., camel-case, is
a practice very similar to hungarian notation.

I would've said something like: start learning the Python
community's naming conventions, and use those instead of
inventing your own.

--
Neil Cerutti
It will work out, somehow. That's a hell of a duo right there, Marbury,
Crawford and Houston. --Carmelo Anthony
Jun 28 '07 #5
Neil Cerutti a écrit :
On 2007-06-28, Bruno Desthuilliers <br********************@wtf.websiteburo.oops.comwr ote:
>Stef Mientki a écrit :
>>How can I list a type of an object instance ?

I tried:

class tLED (tDevice):
<ot>
Do yourself (and the world) a favour and give up hungarian notation...
This should be:

class Led(Device):
#...

Using a naming convention for class objects, e.g., camel-case, is
a practice very similar to hungarian notation.
And it's not even totally consistant since builtin types are usually
lowercase. But it's still the convention.
I would've said something like: start learning the Python
community's naming conventions, and use those instead of
inventing your own.
The TMyType convention comes from Delphi. So let's make this:

"""
start learning the Python
community's naming conventions, and use those instead of the ones of
your usual language
"""

!-)

Jun 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by benyang22 | last post: by
10 posts views Thread by Chris | last post: by
3 posts views Thread by Jason | last post: by
3 posts views Thread by David Jacques | last post: by
3 posts views Thread by sandyw | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.