[SOLVED] I have a problem with a "__call__" attribute...

edited April 2017 in Python Mode

Hello, I'm working on a Physics engine with POO (Python Object Oriented). I have an object problem with a certain "call" attribute.

I have a main file and a file called Physics.py. In Physics.py, I have 3 classes: Object, Vector and Rigidbody.

This is the essential properties of my Object class:

class Object:
def __init__(self, x, y, m):
    self.rigidbody = Rigidbody(x, y, m)
    self.s = 10

This is the essential properties of my Rigidbody class:

class Rigidbody:
def __init__(self, x, y, m):
    self.x = x
    self.y = y
    self.m = m # m is unused yet...
    
    self.velocity = Vector(0, 0)
    self.force = Vector(0, 0)
    self.acceleration = 1
    
def initVelocity(self, x, y):
    self.velocity = Vector(x, y)
    
def force(self, x, y):
    self.force = Vector(x, y)

This is the essential properties of my Vector class:

class Vector:
def __init__(self, x, y):
    self.x = x
    self.y = y
    self.l = sqrt(x**2+y**2)

Behind, I have functions like self.display() that displays a vector or an object on the screen with basic drawing functions. Finally, here is my main file:

from Physics import *

obj = Object(640, 360, 1)
def setup():
    size(1280, 720)
    frameRate(60)
    smooth()
    global obj
    obj.defSize(50)
    obj.defColor([255])

def draw():
    global obj
    background(50)
    obj.rigidbody.force(100, 0)
    obj.display() # This is the display function that I told higher

The force object in the class Rigidbody has other things, but this line in the object is the source of this error:

AttributeError: Vector instance has no attribute '__call__'

When the error comes, Processing highlights this line of code:

    obj.rigidbody.force(100, 0)

which is in the def draw(). But when I print callable(Vector) it writes True! I don't understand why I have to add an attribute call to my class, because in my def force(self, x, y), I define the self.force as a Vector object! Is there a problem? Am I calling the Vector instance as a function? Please, I need help! [-O<

Answers

  • Answer ✓

    Wait, it was just a name problem... I choose the same name for the function force and the self.force property... I renamed the function addforce and it works!

  • edited July 2017
    • You've got a name conflict in your class RigidBody! :-O
    • You've got an instance variable called force & a method also called force()! #-o
    • If your sketch was in Java Mode instead, there'd be no problem, given Java has separate namespaces for variables, methods & classes/interfaces. \m/
    • Nonetheless, even in Java, doing so is frowned upon, although Processing makes such abuses in its own API. 8-X
    # https://forum.Processing.org/two/discussion/21766/
    # i-have-a-problem-with-a-call-attribute#Item_2
    
    # 2017-Apr-02
    
    #add_library('Physics')
    
    def setup():
        size(300, 300)
    
        fill(0xffFFFF00)
        stroke(0)
        strokeWeight(3)
        ellipseMode(CENTER)
    
        global obj
        obj = Object(width>>1, height>>1, height>>1)
        obj.rigidBody.initForce(100, 0)
    
    
    def draw():
        background(050)
        obj.display()
    
    
    class Object:
        def __init__(self, x, y, m):
            self.rigidBody = RigidBody(x, y, m)
            self.s = 10
            self.rigidBody.initForce(100, 0)
    
    
        def display(self):
            rb = self.rigidBody
            ellipse(rb.x, rb.y, rb.m, rb.m)
    
    
    
    class Vector:
        def __init__(self, x=0, y=0):
            self.setXY(x, y)
            self.l = sqrt(x*x + y*y)
    
    
        def setXY(self, x, y): self.x, self.y = x, y
    
    
    
    class RigidBody:
        def __init__(self, x=0, y=0, m=0):
            self.x, self.y, self.m = x, y, m
            self.velocity, self.force = Vector(), Vector()
            self.acceleration = 1
    
    
        def initVelocity(self, x, y): self.velocity.setXY(x, y)
    
        def initForce(self, x, y): self.force.setXY(x, y)
    
  • edited April 2017

    Oops! You've already solved it 20 min ago. I was too l8! :\">

Sign In or Register to comment.