I'm trying to inherit the mechanize class .Browser :

 from mechanize import Browser class LLManager(Browser, object): IS_AUTHORIZED = False def __init__(self, login = "", passw = "", *args, **kwargs): super(LLManager, self).__init__(*args, **kwargs) self.set_handle_robots(False) 

But when I do something like this:

 lm["Widget[LinksList]_link_1_title"] = anc 

The error is:

 Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> lm["Widget[LinksList]_link_1_title"] = anc TypeError: 'LLManager' object does not support item assignment 

And when so:

 >>> m.__setitem__("Widget[LinksList]_link_1_title", anc) >>> print lm.form <TextControl(Widget[LinksList]_link_1_title=Джинсовый чел)> <TextControl(Widget[LinksList]_link_1_url=http://)> 

then it works.

The class __setitem__ method in Browser and above is not overloaded.

Why doesn't my class or instance inherit this method as a parent?

  • Judging by the object, is it about Python-2? - kirelagin
  • Python 2.7, aha. Sorry, I'm a little confused. In this case, we are talking about __getitem__ and __setitem__ , which are not in the entire inheritance chain up to the upper class. - Christ
  • one
    Well, correct the question accordingly. - kirelagin
  • Quite surprisingly ... It seems that it should work ... Now I’ll download this mechanize and try to understand what could have gone wrong ... - kirelagin
  • Previously, I did not inherit classes from Browser all the time, but added an attribute to a class, something like: self.br = Browser ()

1 answer 1

Well, everything is crystal clear. Indeed, in the entire inheritance tree there is no __setitem__ attribute. Instead, the Browser defines a __getattr__ method that checks whether a form is connected to it and, if connected, returns its attribute (that is, it will call __setitem__ this form).

You inherit your class from object , thus it turns into a new-style class , and there everything is already a bit wrong :).


The basic idea is this: it is better not to mix old classes with new ones, otherwise you can run into something;).

  • I went to read man. Can you tell me what to do? The fact is that if I do not inherit from object, then another error occurs ( jezuz-chrizt.livejournal.com/402864.html ) - Christ
  • one
    It's true, super works only with new-style classes :). - kirelagin
  • 2
    You decide whether you want a new-style class or not. If you do not want, then instead of super, specify explicitly the name of the parent class when you want to call the parent method. - kirelagin 4:14
  • so how should i be? Suppose I copy the getattr method from the Browser or I will call it via super. but there is a suspicion that this is not one pitfall. And then either remove the object from the parents and get an error, or do something with what has already been done. How to proceed? - Christ
  • 2
    Well, pass on self to it with the first argument, as requested :) - kirelagin