Posts: 382
Threads: 94
Joined: Mar 2017
Hi all. I am Administrator. With Notepad: I go to correct the error, then I click on "save" icon, then I go to Command Prompt and run with "python Filename".
But with ptpython , where to correct, where to save, where to run.
Beside that , At the bottom, right corner I have "Meta+Enter Execute". What is it ?
Also at the bottom I have "History". I should like to use that, but I don't know how.
Error: C:\Users\Sylvain
λ ptpython
>>> class Selfless:
... def __init__(Self, data):
... self.data=data
... def selfless(arg1,arg2):
... return arg1+arg2
... def normal(self,arg1,arg2):
... return self.data+arg1+arg2
>>> X=Selfless(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __init__
NameError: name 'self' is not defined
name 'self' is not defined
>>>
Posts: 7,091
Threads: 122
Joined: Sep 2016
May-29-2018, 04:43 PM
(This post was last modified: May-29-2018, 04:45 PM by snippsat.)
self is always lowercase.
class Selfless:
def __init__(self, data):
self.data = data
def selfless(self, arg1, arg2):
return arg1 + arg2
def normal(self, arg1, arg2):
return self.data + arg1 + arg2 So now copy into ptpython,just Enter after copy in will execute class Selfless:
C:\Users\Tom
λ ptpython
>>> class Selfless:
... def __init__(self, data):
... self.data = data
...
... def selfless(self, arg1, arg2):
... return arg1 + arg2
...
... def normal(self, arg1, arg2):
... return self.data + arg1 + arg2
>>> # use Class
>>> obj = Selfless(2)
>>> obj.selfless(5, 8)
13
>>> obj.normal(5, 8)
15
>>> Not using self in selfless,use @staticmethod .
Then just a function placed in class Selfless
@staticmethod
def selfless(arg1, arg2):
return arg1 + arg2
Posts: 382
Threads: 94
Joined: Mar 2017
@ snippsat
Thanks. But i should appreciate explanations about "History", and "Meta+Enter -Execute" Is somewhere a User Help ?
Posts: 7,091
Threads: 122
Joined: Sep 2016
Press F3 to start history.
ptpython review
Posts: 382
Threads: 94
Joined: Mar 2017
I cannot copy correctly a console program. First I tried "double click", on forum's page, 2. I I swept all lines, one after the other; Always same bad copy. Here is the copy:
Error: C:\Users\Sylvain
λ class Selfless: def __init__(self, data): self.data = data def selfless(self, arg1, arg2)
: return arg1 + arg2 def normal(self, arg1, arg2): return self.data + arg1 + arg2
'class' is not recognized as an internal or external command,
operable program or batch file.
Posts: 7,091
Threads: 122
Joined: Sep 2016
You have not started ptpython.
Now you have just coped into command line.
There is always >>> when you start.
E:\1py_div\a
λ ptpython
>>> Now first can you copy in code or push F3 for historie as my laste post was about.
Posts: 382
Threads: 94
Joined: Mar 2017
Before pasting i got: Warning! Pasting long text (208 chars) may make the console non-responsive until the paste finishes ! Continue ?
Of course I continued , and here is always the same bad copy:
Error:
C:\Users\Sylvain
λ ptpython
>>> class Selfless: def __init__(self, data): self.data = data def selfless(self, arg1, ar
... g2): return arg1 + arg2 def normal(self, arg1, arg2): return self.data + arg1 + ar
... g2
Posts: 7,091
Threads: 122
Joined: Sep 2016
Copy code from my post and it should work.
This can happens when you copy for some other editors,
just try again with different approach.
Posts: 382
Threads: 94
Joined: Mar 2017
Strange enough! I went to your post. I tried to copy(1 double clic, then 2 line after line) always the same bad copy. Is it possible nobody until now tried to copy a console program ??
Posts: 566
Threads: 10
Joined: Apr 2017
(May-29-2018, 04:43 PM)snippsat Wrote: self is always lowercase.
Formally, you can write it the way you want - or replace with any other name, since self is just a convention. Of course, that would be a very bad idea , and it will piss off any Pythonista reading that blasphemy - but it will work
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|