0

I want my class acts like another class I choose (for the special methods it hasn't)

Here is my class: https://code.sololearn.com/crjlVdwLR3wT/?ref=app

19th Jul 2017, 6:36 PM
clement
clement - avatar
4 Answers
+ 2
Your class already inherits from other class as you declared, class Iter (object), Iter extends object. What do you mean "acts like another class I choose...". Have you tried class Iter(<otherClass>) where <otherClass> is the other class of your choice? Sorry if I misunderstood you.
20th Jul 2017, 3:45 AM
Ipang
+ 2
I must admit that what you're doing is something pretty advanced, and from the external resources (read:stackoverflow:) there's no easy way in achieving your goal. I'm afraid I'm gonna have to leave you with these links I've read, not being ignorant, but I realize that's just as far as I can go. So here they are, bear me if you've already visited them: https://stackoverflow.com/questions/6560354/how-would-i-create-a-custom-list-class-in-JUMP_LINK__&&__python__&&__JUMP_LINK https://stackoverflow.com/questions/9432719/python-how-can-i-inherit-from-the-built-in-list-type https://stackoverflow.com/questions/4093029/how-to-inherit-and-extend-a-list-object-in-python Best of luck,
20th Jul 2017, 10:39 AM
Ipang
0
It was inherits from object to avoid infinite loop since I had redefine __getattribute__. I've done this to detect instance.next but I've just change it (now it is instance.next()). As you've probably see, the class can contains a list or a tuple. So what I want is when I use the instance like a = instance + 2, instance acts like instance.list[instance.index]. I know when I do it, it calls the special method for addition, in I can write : return self[instance.index] + other But I would have to write all the special methods only to redirect...
20th Jul 2017, 6:39 AM
clement
clement - avatar
0
Actualy I need to create a metaclass to intercept the special method calls but I'll have to study how it work. Look at this : https://stackoverflow.com/questions/9057669/how-can-i-intercept-calls-to-pythons-magic-methods-in-new-style-classes
20th Jul 2017, 2:31 PM
clement
clement - avatar