How do you check whether a python method is bound or not?
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to? ...
Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to? ...
I´ve mastered almost all the Python concepts (well, let´s say there are just OO concepts :-)) but this one is tricky. I know it has something to do with introspection but it´s still unclear to me. So what are metaclasses? What do you use them for? Concrete examples, including snippets, much appreciated! ...
So what I'm looking for here is something like PHP's print_r function. This is so I can debug my scripts by seeing what's the state of the object in question. ...
Given the python function: def aMethod(arg1, arg2): pass How can I extract the number and names of the arguments. Ie. given that I have a reference to func, I want the func.[something] to return ("arg1", "arg2") The usage scenario for this is that I have a decorator, and I wish to use the method arguments in the same order that t...
Given an instance of some class in Python, it would be useful to be able to determine which line of source code defined each method and property (e.g. to implement [1]). For example, given a module ab.py class A(object): z = 1 q = 2 def y(self): pass def x(self): pass class B(A): q = 4 def x(self): pass def ...
Why would a coder stuff things into __dict__ that can't be used for attribute access? For example, in my Plone instance, dir(portal) includes index_html, but portal.index_html raises AttributeError. This is also true for the __class__ attribute of Products.ZCatalog.Catalog.mybrains. Is there a good reason why dir() can't be trusted? Pok...
Hi, How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspects module might have helped me out here, but it doesn't seem to give me what I want and short of parsing the __cla...
I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find o...
I'm writing a plugin system for my program and I can't get past one thing: class ThingLoader(object): ''' Loader class ''' def loadPlugins(self): ''' Get all the plugins from plugins folder ''' from diones.thingpad.plugin.IntrospectionHelper import loadClasses classList=loadClasses('./plugin...
In Python, functions like str(), int(), float(), etc. are generally used to perform type conversions. However, these require you to know at development time what type you want to convert to. A subproblem of some Python code I'm trying to write is as follows: Given two variables, foo and bar, find the type of foo. (It is not known at ...
The following code import types class A: class D: pass class C: pass for d in dir(A): if type(eval('A.'+d)) is types.ClassType: print d outputs C D How do I get it to output in the order in which these classes were defined in the code? I.e. D C Is there any way other than using inspect.getsour...
I am working through trying to learn to program in Python and am focused on getting a better handle on how to use Standard and other modules. The dir function seems really powerful in the interpreter but I wonder if I am missing something because of my lack of OOP background. Using S.Lotts book I decided to use his Die class to learn m...
It seems as thought getattribute has only 2 parameters (self, name). However, in the actual code, the method I am intercepting actual takes arguments. Is there anyway to access those arguments? Thanks, Charlie ...
What is the proper way to loop over a Python object's methods and call them? Given the object: class SomeTest(): def something1(self): print "something 1" def something2(self): print "something 2" ...
Hello. In python it's possible to use '.' in order to access object's dictionary items. For example: class test( object ) : def __init__( self ) : self.b = 1 def foo( self ) : pass obj = test() a = obj.foo From above example, having 'a' object, is it possible to get from it reference to 'obj' that is a parent namespace fo...
How can I get the class that defined a method in Python? I'd want the following example to print "__main__.FooClass": class FooClass: def foo_method(self): print "foo" class BarClass(FooClass): pass bar = BarClass() print get_class_that_defined_method(bar.foo_method) ...
Hello there I'm reading How to think like a computer scientist which is an introductory text for "Python Programming". I want to clarify the behaviour of multiply operator (*) when applied to lists. Consider the function make_matrix def make_matrix(rows, columns): """ >>> make_matrix(4, 2) [[0, 0], [0, 0], [0, 0], [0, 0]] >>> m...
I have the following relationships set up: A HABTM B B belongsTo C C hasMany B Now, for a given A, I need all C with the B's attached. I can write the SQL queries, but what's the proper CakePHP way? What method do I call on which model, and with which parameters? ...
This week on comp.lang.python, an "interesting" piece of code was posted by Steven D'Aprano as a joke answer to an homework question. Here it is: class MultiplierFactory(object): def __init__(self, factor=1): self.__factor = factor @property def factor(self): return getattr(self, '_%s__factor' % self.__class_...
I've started to use the python descriptor protocol more extensively in the code I've been writing. Typically, the default python lookup magic is what I want to happen, but sometimes I'm finding I want to get the descriptor object itself instead the results of its __get__ method. Wanting to know the type of the descriptor, or access stat...