The way you've written them, both x & y are static variables of classBall, 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
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. :>
Answers
https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
class
Ball, not instance. =;display(b): ellipse(b.x, b.y, width>>1, height>>1)
__init(b)__: b.x = 0; b.y = mouseY
__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
class
PApplet. #-oclass
PApplet is automatically instantiated by the "sketch" itself. :-bdclass
were static, I've concluded that was unintentional. /:)https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
no of course mouseY is not static. x and y is.
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
However it compiles when other
static
fields, like WINDOWS, LINUX & MACOSX, etc., are used instead. :>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