Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
It seems like there is a problem running Pythonista 3 def in iOS12.1 public beta2
-
@cvp Thanks! It works! I am a beginner of python. Could you tell me why there must be self? When I code on my Mac, it could run without self, but when I code in pythonista3 on my phone, it must run with writing self.why? Look forward to your replying.
-
@BarryZ I never could better explain than a lot of web sites you can find with Google « python self »...
-
@BarryZ Are you sure that you're running exactly the same code on your Mac and in Pythonista, especially the part that you type into the Python console? There is an important difference between
q = Quadratic_Functions_4_1
andq = Quadratic_Functions_4_1()
(note the parentheses at the end).Also, it looks like you might not need to use a class here at all. Your current code would work just fine if you move the functions out of the class, then you can use
QFY(1, 2, 3, 4)
directly (withoutq
before it). -
@dgelessus Thanks for your suggestions. yeah, I am sure. On my Mac, I use python 3.7.x to code. I don’t know why.
-
@cvp Thanks!
-
-
@cvp There is no @staticmethod.
On my Mac:
class Quadratic_Functions_4_1:
def QFY(x,a,b,c):
y=ax**2+bx+c
print('the y is',y)Run on my Mac:
Q=Quadratic_Funtions_4_1()
Q.QFY(1,4,8,10)
the y is 22 -
@BarryZ I understand, what I wanted to say is that the Python you use on your Mac perhaps defines the class methods as static by default...
-
@cvp Besides, when I code like this:
class quadratic_functions:
def qfx(self,y,a,b,c):
delta=b2-4a(c-y)
if delta==0:
x=-b/2*a
print('the x is',x)
if delta<0:
print('there is no solution for x.')
else:
x1=x=(-b+delta(1/2))/(2a)
x2=x=(-b-delta**(1/2))/(2a)
print('x1 is',x1)
print('x2 is',x2)When I run it on my Mac and pythonista3:
q=quadratic_functions()
q.qfx(1,4,8,10)
there is no solution for x.It works.
Or when I code like this:
class quadratic_functions:
def qfx(y,a,b,c):
delta=b2-4a(c-y)
if delta==0:
x=-b/2*a
print('the x is',x)
if delta<0:
print('there is no solution for x.')
else:
x1=x=(-b+delta(1/2))/(2a)
x2=x=(-b-delta**(1/2))/(2a)
print('x1 is',x1)
print('x2 is',x2)When I run it on my Mac and pythonista3:
q=quadratic_functions
q.qfx(1,4,8,10)
there is no solution for xIt works.
-
@BarryZ That's normal. if you use an instance of the class (with parentheses), you need to have the word "self". But without parentheses, you use it as a class, thus no "self" needed.
I think I can't help you more, I'm not a Python specialist but in this forum I'm sure other guys could help you. -
@cvp Thanks! You have already helped me a lot! I will search it on the internet to figure it out.
-
@BarryZ Perhaps does your Python interpreter recognize that these methods do not use any variables of the class it-self, nor the instance (self) thus could automatically consider the method as static...
-
This post is deleted! -
The difference is that in one case you are instantiating an object.
q=quadratic_function()
In the other case, you are referring to the class:
q=quadratic_function
These are two completely different things. The first creates an instance, and binds the instance to the first argument, thus requires
self
as first argument. The second does not, so does not need aself
. -
@JonB Yes, I know but that doesn't explain the difference between Pythonista and his Mac
-
my guess is copy paste error -- the original probably did not have the parens.
what version of python are you running on the mac?
are you using the console, or executing a file?
are you using jupyter or IPython or some other nonstandard interpreter?The only other thought... since your class does not inherit from object, on 2.x this will be an "old style class", so might behave a little differently. But I think even old style classes would have bound instances to their methods.