listing the type of an object

S

Stef Mientki

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
 
T

Thomas Jollans

Stef said:
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)

Ahhh, history. At the moment (that is in 2.2 <= Python Version < 3.0 ),
there are two types of classes: old-style classes are the default and a
load of WTF?, which new-style classes make sense and behave like you
(that is "one" in general and you, Stef, in particular) expect.

As I'm too lazy to explain what all this is about, I'll point you to a
page with a promising title:

http://wiki.python.org/moin/NewClassVsClassicClass

Thomas Jollans


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGgtTeJpinDvQhQ0sRArVwAJ4+kXXUX7j96d1PCOQit7kbpTqZ/gCfSPzO
vZPfP/t2DKPV2Ahj6JO1Qns=
=k4BR
-----END PGP SIGNATURE-----
 
B

Bruno Desthuilliers

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):
#...

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()
 
S

stef

Bruno said:
Stef Mientki a écrit :

<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.
looks like Device is an old style class.


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
 
B

Bruno Desthuilliers

stef a écrit :
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...
 
N

Neil Cerutti

Stef Mientki a écrit :

<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.
 
B

Bruno Desthuilliers

Neil Cerutti a écrit :
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
"""

!-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top