Giter VIP home page Giter VIP logo

Comments (5)

dfunckt avatar dfunckt commented on June 8, 2024

This can be confusing but is normal Python behaviour. f is indeed the undecorated function, so it obviously has no context attribute. On the other hand, pred_f should have the context attribute.

from django-rules.

perezzini avatar perezzini commented on June 8, 2024

So, if I'd like to use Predicate to wrap a function definition, I'd have to define f like the following?:

def f(self, a):
    self.context["v"] = 3
    return True

pred_f = Predicate(f, bind=True)

pred_f.test(5)  # works correctly, returns True

The latter is the approach I must take in this case, right? Or is it another work-around (I'd like to not use the predicate decorator to wrap arbitrary functions into rules's predicates)?

Thanks in advance for the help!

from django-rules.

dfunckt avatar dfunckt commented on June 8, 2024

It's totally fine to use the Predicate class directly instead of the decorator. This snippet looks good to me.

from django-rules.

dfunckt avatar dfunckt commented on June 8, 2024

On the other hand, if the function tries to access self.context it's not really just an "arbitrary function" -- it has explicit knowledge of rules and predicates. I don't know what you're trying to do, but my point is maybe wrapping the original function f as a predicate is the best course of action -- plain invocations of it will throw an exception, as you found out -- so it can't be used for much else than a predicate.

from django-rules.

perezzini avatar perezzini commented on June 8, 2024

Yes, you're right: it's not an "arbitrary function". The problem is that I'm currently in an scenario where we consume function definitons on-the-fly, and them could try to access context. So the better approach, I think, is wrapping them using Predicate(f, bind=True) class (obviously, each function definition must have a signature like def f(self, ...) and access context like self.context).

Btw, I'm using rules as a generic framework for a rule-based system.

Thanks for your quick response @dfunckt !

from django-rules.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.