mouse not recognised in class

edited October 2017 in Python Mode

putting

**_class Ball: x=0 y=mouseY

ball = Ball()_**

at the top of a program tells me that mouseY not recognised. Why?

Answers

  • edited October 2017

    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    • The way you've written them, both x & y are static variables of class Ball, not instance. =;
    • In Python, instance (non-static) members are prefixed w/ their method's 1st parameter.
    • Let's say you've got a method called display(). And it's 1st parameter is named as b:
      display(b): ellipse(b.x, b.y, width>>1, height>>1)
    • Also, the appropriate place to initialize instance variables is inside constructor __init()__: L-)
      __init(b)__: b.x = 0; b.y = mouseY
    • Although I've used b as the name of the 1st parameter for the class' methods above, the "pythonic" way is always name them as self. ;;)
      __init(self)__: self.x = 0; self.y = mouseY
  • actually i know py. well my question is : how do you declare the keyword inside the class

    the way i wrote that was to pose the question. Actually self.x is not more pythonic. it depends on your reasons. i specifically needed static variables for quick prototyping. again init ... hum please this is a processing forum not python one

  • edited October 2017

    I specifically needed static variables for quick prototyping.

    • Variable mouseY ISN'T static! It is an instance member of class PApplet. #-o
    • class PApplet is automatically instantiated by the "sketch" itself. :-bd
    • However, that happens later on, a little before invoking the 1st callback preload(). :-B
    • The moment the static members of your class are initialized is too early. :-O
  • edited October 2017

    Actually I know py. well.
    The way I wrote that was to pose the question.

    • A big portion of the folks who come here everyday doesn't even know the basic. :-<
    • We can only judge by the way the code is presented at face-value. 3:-O
    • So, when I've spotted the members x & y in your Ball class were static, I've concluded that was unintentional. /:)
    • BtW, you haven't yet edited your post in order to format it in accordance to markdown: :-@

    https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

  • Actually self.x is not more pythonic.

    • I dunno much Python. Just enough to convert Java Mode to Python Mode safely. ~O)
    • But AFAIK, even though self isn't a Python keyword, it is a "sacred" naming convention. :@)
  • no of course mouseY is not static. x and y is.

  • edited April 2018

    But you're attempting to initialize your static variable y using the current value of an instance variable! :-@

    Under Java, that wouldn't even compile! It'd display the following compile error: :-&
    Cannot make a static reference to the non-static field mouseY

    void setup() {
      println(new Ball().y);
      exit();
    }
    
    static class Ball {
      //static int x = WINDOWS, y = LINUX; // This 1 compiles.
      static int x = MACOSX, y = mouseY; // This 1 won't!
    }
    

    However it compiles when other static fields, like WINDOWS, LINUX & MACOSX, etc., are used instead. :>

  • edited October 2017

    I know Python doesn't prohibit (or even knows their diff.) static members to use instance members. :-\"

    But as I've already pointed out, mouseY doesn't even exist at the time you're attempting to assign it to static member Ball::y. 8-}

  • XD you added more emojies etc since last visit ^^

  • Related: https://forum.processing.org/two/discussion/comment/122852/#Comment_122852

    The best practice is to init your variables within setup().

    Kf

Sign In or Register to comment.