473,387 Members | 1,512 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,387 software developers and data experts.

Re: generate methods at runtime, but the wrong one gets called

Le Monday 25 August 2008 11:37:23 Steven Samuel Cole, vous avez écrit*:
Hello,

I am writing an application that controls robots. Different robots can
do different kinds of movements, such as e.g. open gripper, rotate
gripper, etc. My RobotControl class should support all kinds of
robots. I therefore delegate the actual control work to extra
control-specific classes, one class per movement type, e.g.
OpenGripperControl, RotateGripperControl. These movement control
classes are created by a simple class factory.
i don't get your design, it seems over-complicated to mee at first glance.
This is my class half-way through:
...
>
I tried this in class RobotControl in __init__() in the 'for
movementType ...' loop:

funcName = 'Set' + movementType
function = lambda self, value:
self.__setMovementTypeValue(movementType, value)
method = new.instancemethod(function, self, self.__class__)
setattr(self, funcName, method)

and the code somewhat seems to work, but instead of SetOpenGripper,
SetRotateGripper is called.
The free variable movementType in the lambda is evaluated lately as it werein
its last state once you return from __init__, if you want to early bind it to
its value in each step of a for loop, you must use :

func = lambda s, v, m_t=movementType : s.__setMovementTypeValue(m_t, v)
My questions:

1.) Does this look like a somewhat reasonable approach to someone who
knows more about Python than me ?
At first, what you do is creating instancemethod and bound them to an instance
of your class, I find this confusing. Why not just use __getattr__ special
method ?

(you don't need __setMovementTypeValue in this example)

def __getattr__(self, name) :
if name.startswith('Set') :
movement = name.lstrip('Set')
if movement in self.__controls :
return lambda value : self.__controls[movement].SetValue(value)
raise AttributeError

That said, if I understand you well, your class RobotControl seems to contain
only logic that is not specific to an instance, nor to a class of instances.
In other OOP language I would encourage you to implement this logic in some
sort of singleton, but in python we don't like this construct, module level
variables and function do perfectly the job, simple as they are.
2.) What could I be doing wrong ?

I have a suspicion that my error is not even related to all this fancy
runtime code generation stuff, but something really dumb and I've just
been coding for too long to see it.

http://mail.python.org/pipermail/pyt...ne/446601.html
shows a somewhat comparable constellation and it was a good guideline.
But there, the function is created by the class factory, as well and I
unfortunately can't do that.

Thank you very much,

Steve
--
http://mail.python.org/mailman/listinfo/python-list


--
_____________

Maric Michaud
Aug 25 '08 #1
2 970
On Mon, 25 Aug 2008 12:52:44 +0200, Maric Michaud wrote:
In other OOP language I would encourage you to implement this logic in
some sort of singleton, but in python we don't like this construct,
module level variables and function do perfectly the job, simple as
they are.
Modules are some sort of singletons in Python.

Ciao,
Marc 'BlackJack' Rintsch
Aug 25 '08 #2
Maric Michaud a écrit :
(snip)
i don't get your design, it seems over-complicated to mee at first glance.
<aol />
Aug 26 '08 #3

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

Similar topics

5
by: Bob Bamberg | last post by:
Hello All, I have been trying without luck to get some information on debugging the Runtime Error R6025 - Pure Virtual Function Call. I am working in C++ and have only one class that is derived...
1
by: Peter Newman | last post by:
I'm embedding a Python interpreter into a project, and compiling with MSVC. It all runs great in the "Debug" configuration, but everything gets scary when I switch to "Release". I've read about...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
17
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
7
by: Jay Loden | last post by:
Hi all, First, apologies if anyone gets this twice, but it took me quite a while to figure out that Python.org is evidently rejecting all mail from my mail server because I don't have reverse...
0
by: Steven Samuel Cole | last post by:
Hello, I am writing an application that controls robots. Different robots can do different kinds of movements, such as e.g. open gripper, rotate gripper, etc. My RobotControl class should...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.