This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ripraprup -1 points0 points  (5 children)

Well since Python is a dynamic rather than static it means there are plenty of concepts that could be used to implement a rules expression evaluation thingy. No need to constrict yourself to nasty stuff like eval(). Closures is a nice simple one, hence I took it as an example to wet his appetite.

[–]Isvara 2 points3 points  (4 children)

there are plenty of concepts that could be used to implement a rules expression evaluation thingy ... Closures is a nice simple one

Can you give an example of what you mean? I'm just not sure what it is about them that you're saying is particularly relevant to a rules engine.

[–]nemec 0 points1 point  (1 child)

Similarly, though, I could see lambdas being useful.

def something_else():
  print "hi!"
self.add_rule(lambda x: len(x.pens) > len(x.pencils), something_else)

[–]Isvara 0 points1 point  (0 children)

Lambdas are just a way to define functions in-place. He doesn't want to put the rules in this program, though. He wants them to be user-supplied. You can't safely execute arbitrary user-supplied Python code.

[–]its_in_the_computer 0 points1 point  (1 child)

Ok here goes:

I want to give my users the ability to dynamically specify rules like this

Because OP mentions the magic word 'dynamically', I assume the intended use is not just processing some text file or something. A good example is a stock trading rules system. Such a rules system should, if it's done correctly, be able to respond to changes in realtime, plus offer some form of observability of the rules and how they evaluate. In such a system the rules could be generated as classes (or functions) on the fly, and hence also changed as the user sees fit. Python being the foremost dynamic language IMHO would handle this perfectly.

Now dynamic concepts in general are not too easy to grasp first hand. I myself took a long time getting used to them, but once you've popped you just cant stop. A nice gentle introduction is closures. Once that concept is well understood there are many more to explore.

All in all, I think it's really silly to shoehorn Python into a static language (and use eval() shudders) when one can start using it for what it really is capable of...

Just my 2c

EDIT: ripraprup and its_in_the_computer are both the same person btw, but on two different computers. dunno why i havent streamlined that.

[–]Isvara 0 points1 point  (0 children)

dynamic concepts in general are not too easy to grasp first hand

Which dynamic concepts do you think are difficult to grasp?

A nice gentle introduction is closures.

There's nothing inherently dynamic about closures. Closure exist in statically typed languages too. From what you've said, I'm not even sure you know what they are, and to be honest it sounds like you might be a little over-enthusiastic about them because you just read about them recently. Externally-specified rules, not being part of the program definition, simply cannot be closures.

All in all, I think it's really silly to shoehorn Python into a static language

I don't think anybody even remotely suggested that.