473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Python doesn't know class of function ? : class C:def f():pass;l=[f]; print C.l[0].im_class

Python doesn't know the class of a method when container not direct
class attribute:
class X: .... def f():pass
.... g=f
.... l=[f]
.... X.g <unbound method X.f> X.l[0] <function f at 0x01A9E1F0> X.l[0].im_class Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'function' object has no attribute 'im_class'

why is l[0] a <function>. Any possibility to find the class of this
beast dynamically?

Robert
Jul 18 '05 #1
3 3016

"Robert" <k.******@gmx.de> wrote in message
news:19**************************@posting.google.c om...

Functions do not have a class.
Python doesn't know the class of a method when container not direct
class attribute:
class X: ... def f():pass
... g=f
at this point, f and g are both *functions*
... l=[f]
and l is a list containing function f
... X.g <unbound method X.f>
When you ask for attribute g and function g is found, g gets wrapped and
returned as an unbound method.
X.l[0] <function f at 0x01A9E1F0>


l is a list so no wrapping is done
X.l[0].im_class

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'function' object has no attribute 'im_class'

why is l[0] a <function>. Any possibility to find the class of this
beast dynamically?


To repeat, functions do not have a class. Any function defined anywhere
can be made an attribute of multiple classes (that do not have attribute
addition turned off).

Terry J. Reedy


Jul 18 '05 #2

"Robert" <k.******@gmx.de> wrote in message
news:19**************************@posting.google.c om...
Python doesn't know the class of a method when container not direct
class attribute:
class X: ... def f():pass
... g=f
... l=[f]
... X.g <unbound method X.f> X.l[0] <function f at 0x01A9E1F0> X.l[0].im_class Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'function' object has no attribute 'im_class'


why is l[0] a <function>. Any possibility to find the class of this
beast dynamically?


As Terry said, this is not possible because of the highly
dynamic nature of Python.

If you'd tell us a bit about what you're trying to accomplish
by relating functions to the class where they're defined, we
might be able to suggest some other ways of approaching
the problem.

John Roth
Robert

Jul 18 '05 #3
Hello, its a problem which arised within my proprietary context test
system (i don't like the awkward stiff unittest). e.g.:

def f():
"test dummy"
print dscore.cfg.url_categories
runTests(f)
class TG(TestGroup):
contexts = [CtxRegistry]
def testA(self):
print "A"
def testB(self):
print "B"
tests=[testA,testB,f]
runTests( TG, [CtxGerman,CtxLocalCache] )
the TG member "tests=[testA,testB,f]" defines (additional) embedded
tests in TG, and optionally also forces the execution order of
(autocollected) textXXXX methods of the own class. I want to script
such tests short and pythonic, thus mix functions, instance method,
unbound methods, static methods, groups, ... the test comiler is
supposed to smartly identifying everything and do whats needed on the
fly.

So, when iterating the list in my test compiler, i need to know if the
function is a simple function from outside or an own method or a
method of other class/instance or whatever in order to possibly
instantiate the right classes on the fly an call the right method,
etc. The are also many other reasons why it is interesting to know the
class of such method-functions.

What I do now is to check each thing in the list if it possibly
matches a method of the own class by comparing against
getattr&method.im_func==thing. Not very elegant.
And if the method-function is coming similarly but indirectly from
another class(es list) i have no chance at all to detect the class...

I think when python compiles the TG class, python should know that the
testA thing is in class TG namespace (and set something like im_class)
?
Strange that below example X.g yields an <unbound method>, but inside
namesspace (tests list) its a mere <function> with no indication of
class(es namespace). could a class indicator not be set similar like
func_globals !?

I think this is somewhat broken design in python classes in order to
optimize compilation speed or so. I think they did that 'economical
implementation' because of the same reason that causes the following
not to work:
class YYY:
.... print YYY
....
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 2, in YYY
NameError: name 'YYY' is not defined

I remember i missed this capability often too.

Is there a true reason not to know the class thing while its namespace
is set up?

I suppose it would be possible for the compiler to do a simple setting
of the empty YYY class object to the outside global namespace before
compiling the interior and maybe set also a local reflexive __class__
or so to the class namespace locals(). (garbage collection
inefficient?afraid of exceptions?its the same as with modules i think)
Thus it would be at least possible to ...

tests=[TG.testA,TG.testB,f]

And also compiling an im_class for unbound methods during class
compiliation would also be no problem.
I think that would be more consistent than seeking for functions in in
the locals() at the end of class definition in order to make them
<unbound methods> later - and forget my lovely tests=[...] ... ?

Robert

"John Roth" <ne********@jhrothjr.com> wrote in message news:<10*************@news.supernews.com>...
"Robert" <k.******@gmx.de> wrote in message
news:19**************************@posting.google.c om...
Python doesn't know the class of a method when container not direct
class attribute:
>> class X:

... def f():pass
... g=f
... l=[f]
...
>> X.g

<unbound method X.f>>> X.l[0] <function f at 0x01A9E1F0>>> X.l[0].im_class

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'function' object has no attribute 'im_class'
>>

why is l[0] a <function>. Any possibility to find the class of this
beast dynamically?


As Terry said, this is not possible because of the highly
dynamic nature of Python.

If you'd tell us a bit about what you're trying to accomplish
by relating functions to the class where they're defined, we
might be able to suggest some other ways of approaching
the problem.

John Roth

Robert

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ali El Dada | last post by:
hi all: in python, when a class, say Father, has a member that itself is an instance of another class, say Child, an instance of Father actually only has a reference to a Child, not the Child...
39
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
11
by: Nicolas Lehuen | last post by:
Hi, I hope this is not a FAQ, but I have trouble understanding the behaviour of the super() built-in function. I've read the excellent book 'Python in a Nutshell' which explains this built-in...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
8
by: Fuzzyman | last post by:
Hello all, I may well be being dumb (it has happened before), but I'm struggling to fix some code breakage with Python 2.6. I have some code that looks for the '__lt__' method on a class: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.