Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > __dict__ for instances?

Reply
Thread Tools

__dict__ for instances?

 
 
Ivan Voras
Guest
Posts: n/a
 
      05-13-2007
While using PyGTK, I want to try and define signal handlers
automagically, without explicitly writing the long dictionary (i.e. I
want to use signal_autoconnect()).

To do this, I need something that will inspect the current "self" and
return a dictionary that looks like:

{
"method_name" : self.method_name
}

Class.__dict__ does something very similar, but when I use it, either
I'm doing something wrong or it doesn't return methods bound to "self",
and python complains a wrong number of arguments is being passed to the
methods (one instead of two).

instance.__dict__ on the other hand returns an empty dictionary.

This looks like it should be easy, but I can't find the solution

--
(\__/)
(O.o)
(> < )

This is Bunny.
Copy Bunny into your signature to help him on his way to world domination!


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

iD8DBQFGRlnFldnAQVacBcgRAugIAJ0aqM6ZNKeUZUFCur7ODE bGcMFvCgCgxze2
LCUqdGi4nCvZrwnFru2To6Y=
=+8Zd
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
half.italian@gmail.com
Guest
Posts: n/a
 
      05-13-2007
On May 12, 5:20 pm, Ivan Voras <ivoras@__fer.hr__> wrote:
> While using PyGTK, I want to try and define signal handlers
> automagically, without explicitly writing the long dictionary (i.e. I
> want to use signal_autoconnect()).
>
> To do this, I need something that will inspect the current "self" and
> return a dictionary that looks like:
>
> {
> "method_name" : self.method_name
>
> }
>
> Class.__dict__ does something very similar, but when I use it, either
> I'm doing something wrong or it doesn't return methods bound to "self",
> and python complains a wrong number of arguments is being passed to the
> methods (one instead of two).
>
> instance.__dict__ on the other hand returns an empty dictionary.
>
> This looks like it should be easy, but I can't find the solution
>
> --
> (\__/)
> (O.o)
> (> < )
>
> This is Bunny.
> Copy Bunny into your signature to help him on his way to world domination!
>
> signature.asc
> 1KDownload


I think you want "dir(instance)" __dict__ returns the instance
variables and values as a dictionary, but doesn't return methods.
dir() returns a list of the instance's methods and variables. Then
you'd need to iterate over the list with type() looking for instance
methods

instance = Class.Class()
dict = {}
methods = [f for f in dir(instance) if str(type(instance.f)) == "<type
'instancemethod'>"]
for m in methods:
dict[m.name] = m

The above is untested. I'm sure there is a better way to do this.

~Sean

 
Reply With Quote
 
 
 
 
Ivan Voras
Guest
Posts: n/a
 
      05-13-2007
wrote:

> I think you want "dir(instance)" __dict__ returns the instance


Part of the problem is that dir(instance) returns a list of strings, so
iterating the dir(instance) gets me strings, not methods. Alternatively,
is there a way to get a "bound" instance by its name - some
introspection function perhaps?

> variables and values as a dictionary, but doesn't return methods.


It does on a Class

--
(\__/)
(O.o)
(> < )

This is Bunny.
Copy Bunny into your signature to help him on his way to world domination!


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

iD8DBQFGRvbdldnAQVacBcgRAvtoAJ46Z2Tkwo+MS6khyh9UZ4 w2vqbccQCg11yr
SbMsN4ezb8lWw3Yz3evcDAc=
=dxlb
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      05-13-2007
Ivan Voras a écrit :
> wrote:
>
>
>>I think you want "dir(instance)" __dict__ returns the instance

>
>
> Part of the problem is that dir(instance) returns a list of strings, so
> iterating the dir(instance) gets me strings, not methods. Alternatively,
> is there a way to get a "bound" instance by its name - some
> introspection function perhaps?


getattr(obj, name)

>
>>variables and values as a dictionary, but doesn't return methods.

>
>
> It does on a Class
>

Usually, methods are attributes of the class, not of the instance.
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      05-13-2007
Ivan Voras a écrit :
> While using PyGTK, I want to try and define signal handlers
> automagically, without explicitly writing the long dictionary (i.e. I
> want to use signal_autoconnect()).
>
> To do this, I need something that will inspect the current "self" and
> return a dictionary that looks like:
>
> {
> "method_name" : self.method_name
> }
>
> Class.__dict__ does something very similar, but when I use it, either
> I'm doing something wrong or it doesn't return methods bound to "self",
> and python complains a wrong number of arguments is being passed to the
> methods (one instead of two).


You're not doing anything wrong, that's just how Python works. "methods"
are wrapper objects around function objects attributes. The wrapping
only happens at lookup time, and returns different kind of "method"
wrapper (resp. unbound or bound methods) if the attribute is looked up
on an instance or a class (there are also the staticmethod/classmethod
things, but that's not your problem here).

You can build the dict you're looking for using dir() and getattr():

from types import MethodType

autoconnect_dict = {}
for name in dir(self):
attr = getattr(self, name)
if isinstance(attr, MethodType):
autoconnect_dict[name] = attr
return autoconnect_dict


> instance.__dict__ on the other hand returns an empty dictionary.


the instance's __dict__ only stores per-instance attributes. While it's
technically possible to attach methods per-instance, it's definitively a
corner case.
 
Reply With Quote
 
Marc Christiansen
Guest
Posts: n/a
 
      05-13-2007
Ivan Voras <ivoras@__fer.hr__> scribis:
> While using PyGTK, I want to try and define signal handlers
> automagically, without explicitly writing the long dictionary (i.e. I
> want to use signal_autoconnect()).
>
> To do this, I need something that will inspect the current "self" and
> return a dictionary that looks like:
>
> {
> "method_name" : self.method_name
> }


Nope, at least for PyGTK 2 See below.

[...]
> This looks like it should be easy, but I can't find the solution


Use the doc, Luke, oops, Ivan
Citing the gtk.glade.XML.signal_autoconnect documentation:
def signal_autoconnect(dict)
dict: a mapping or an instance
^^^^^^^^

The signal_autoconnect() method is a variation of the
gtk.glade.XML.signal_connect method. It uses Python's introspective
features to look at the keys (if dict is a mapping) or attributes (if
^^^^^^^^^^^^^^
dict is an instance) and tries to match them with the signal handler
^^^^^^^^^^^^^^^^^^^
names given in the interface description. The callbacks referenced by
each matched key or attribute are connected to their matching signals.
The argument is called dict due to compatibility reasons since
originally only the mapping interface was supported. The instance
variant was introduced in PyGTK 2.0.

So simply using signal_autoconnect(self) should work.

AdiaÅ*, Marc
 
Reply With Quote
 
half.italian@gmail.com
Guest
Posts: n/a
 
      05-13-2007
On May 13, 4:30 am, Ivan Voras <ivoras@__fer.hr__> wrote:
> half.ital...@gmail.com wrote:
> > I think you want "dir(instance)" __dict__ returns the instance

>
> Part of the problem is that dir(instance) returns a list of strings, so
> iterating the dir(instance) gets me strings, not methods. Alternatively,
> is there a way to get a "bound" instance by its name - some
> introspection function perhaps?
>
> > variables and values as a dictionary, but doesn't return methods.

>
> It does on a Class
>
> --
> (\__/)
> (O.o)
> (> < )
>
> This is Bunny.
> Copy Bunny into your signature to help him on his way to world domination!
>
> signature.asc
> 1KDownload


I tried.

~Sean

 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      05-13-2007
Marc Christiansen wrote:

> Nope, at least for PyGTK 2 See below.


Aaah, but....!

> [...]
>> This looks like it should be easy, but I can't find the solution

>
> Use the doc, Luke, oops, Ivan
> Citing the gtk.glade.XML.signal_autoconnect documentation:
> def signal_autoconnect(dict)
> dict: a mapping or an instance
> ^^^^^^^^


I should have mentioned - I tried it already and it didn't work. The
specific error I get is:

"WARNING: "on_button_clicked" not callable or a tuple"

once for each handler when I call autoconnect. And I've got a recent
version of pyGTK (2.10.4) so it should.


--
(\__/)
(O.o)
(> < )

This is Bunny.
Copy Bunny into your signature to help him on his way to world domination!


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

iD8DBQFGR0jOldnAQVacBcgRAvHqAKCdYJxSmCqvyrBXVTPQMc 50R5KsMwCfULkH
jpSJzgL8qBJRaQIP8xKQrXA=
=SsEg
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      05-13-2007
Bruno Desthuilliers wrote:

> You're not doing anything wrong, that's just how Python works. "methods"
> are wrapper objects around function objects attributes. The wrapping
> only happens at lookup time, and returns different kind of "method"
> wrapper (resp. unbound or bound methods) if the attribute is looked up
> on an instance or a class (there are also the staticmethod/classmethod
> things, but that's not your problem here).


Got it, thanks for the explanation!



--
(\__/)
(O.o)
(> < )

This is Bunny.
Copy Bunny into your signature to help him on his way to world domination!


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

iD8DBQFGR0kPldnAQVacBcgRAls/AJ9vyGQG9Hvasr0Qex5gTl+wDD3gTgCeNBo0
GQNo30dEuhwVZaxfHitgBv4=
=8ZXE
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      05-13-2007
Ivan Voras a écrit :
> Marc Christiansen wrote:
>
>
>>Nope, at least for PyGTK 2 See below.

>
>
> Aaah, but....!
>
>
>>[...]
>>
>>>This looks like it should be easy, but I can't find the solution

>>
>>Use the doc, Luke, oops, Ivan
>>Citing the gtk.glade.XML.signal_autoconnect documentation:
>> def signal_autoconnect(dict)
>> dict: a mapping or an instance
>> ^^^^^^^^

>
>
> I should have mentioned - I tried it already and it didn't work. The
> specific error I get is:
>
> "WARNING: "on_button_clicked" not callable or a tuple"


Please post the relevant code and the full traceback.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__slots__ vs __dict__ Jean Brouwers Python 5 05-13-2004 04:20 PM
RE: __slots__ vs __dict__ Hornberger, Chris Python 1 05-12-2004 09:35 PM
When is a __dict__ not a __dict__? Derek Fountain Python 1 04-21-2004 10:31 AM
__slots__ replacing __dict__ function anabell@sh163.net Python 1 11-06-2003 09:16 AM
obj.__dict__ expected behavior or bug? Ed Young Python 4 08-10-2003 10:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57