[Tutor] inquire

XIA Xiao-Qin XQ.Xia@ccsr.cam.ac.uk
Mon, 15 Jan 2001 18:38:03 +0000


--=====_97958388318467=_
Content-Type: text/plain; charset="WINDOWS-1250"
Content-Transfer-Encoding: quoted-printable

Dear Sir / Madam,

Excuse me -- I have questions again.

1.Is there any way to know the type of a variable in Python.
for example:

def deal(*options):
    for k in options:
        if k is a list:    # Can I get to know the type of k?
            my_function_for_list()
        elif k is a class:
            my_function_for_class()

2. Can a method know the name of the class in which the method is defined?

class A:
   def fun(self):
      print self.__class__.__name__    #  perhaps print 'A' or ''D', ...
      print A.fun.im_class.__name__    # Is here any way to print 'A' at=
 any time without using A directly?
  
class B(A):
   pass
  
class C(B):
   pass

class D(C):
   def newfun(self):
      B.fun(self)

And can we print the name of class B? by modify the methods fun(self) and=
 newfun(self)?


Sincerely yours,
Xia XQ


--=====_97958388318467=_
Content-Type: text/html; charset="WINDOWS-1250"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content="text/html; charset=windows-1250" http-equiv=Content-Type>
<META content='"MSHTML 4.72.3612.1700"' name=GENERATOR>
</HEAD>
<BODY bgColor=#ffffff style="FONT-FAMILY: Arial" text=#000000>
<DIV><FONT color=#000000 size=2>Dear Sir / Madam,</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000 size=2>Excuse me -- I have questions 
again.</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>1.Is there any way to know the type of a variable in 
Python.</FONT></DIV>
<DIV><FONT size=2>for example:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>def deal(*options):</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp; for k in options:</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if k is a 
list:&nbsp;&nbsp;&nbsp; # Can I get to know the type of k?</FONT></DIV>
<DIV><FONT 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
my_function_for_list()</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif k is a 
class:</FONT></DIV>
<DIV><FONT 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
my_function_for_class()</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>2. Can a method know the name of the class in which the method 
is defined?</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV>class A:<BR>&nbsp;&nbsp; def fun(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
print self.__class__.__name__&nbsp;&nbsp;&nbsp; #&nbsp; perhaps print 'A' or 
''D', ...</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
A.fun.im_class.__name__&nbsp;&nbsp;&nbsp; # Is here any way to print 'A' at any 
time without using A directly?</DIV>
<DIV>&nbsp; <BR>class B(A):</DIV>
<DIV>&nbsp;&nbsp; pass</DIV>
<DIV>&nbsp; <BR>class C(B):<BR>&nbsp;&nbsp; pass</DIV>
<DIV>&nbsp;</DIV>
<DIV>class D(C):<BR>&nbsp;&nbsp; def 
newfun(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B.fun(self)<BR></DIV>
<DIV><FONT size=2>And can we print the name of class B? by modify the methods 
fun(self) and newfun(self)?</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Sincerely yours,</FONT></DIV>
<DIV><FONT size=2>Xia XQ</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV></BODY></HTML>


--=====_97958388318467=_--