Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66734 > unrolled thread
| Started by | ApathyBear <nirchernia@gmail.com> |
|---|---|
| First post | 2014-02-19 23:32 -0800 |
| Last post | 2014-02-20 02:13 -0800 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.python
Cannot figure out line of code, also not understanding error ApathyBear <nirchernia@gmail.com> - 2014-02-19 23:32 -0800
Re: Cannot figure out line of code, also not understanding error Chris Angelico <rosuav@gmail.com> - 2014-02-20 19:03 +1100
Re: Cannot figure out line of code, also not understanding error Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2014-02-20 08:56 +0100
Re: Cannot figure out line of code, also not understanding error ApathyBear <nirchernia@gmail.com> - 2014-02-20 00:26 -0800
Re: Cannot figure out line of code, also not understanding error Chris Angelico <rosuav@gmail.com> - 2014-02-20 19:54 +1100
Re: Cannot figure out line of code, also not understanding error Gary Herron <gary.herron@islandtraining.com> - 2014-02-20 01:04 -0800
Re: Cannot figure out line of code, also not understanding error ApathyBear <nirchernia@gmail.com> - 2014-02-20 01:22 -0800
Re: Cannot figure out line of code, also not understanding error Chris Angelico <rosuav@gmail.com> - 2014-02-20 20:41 +1100
Re: Cannot figure out line of code, also not understanding error Dave Angel <davea@davea.name> - 2014-02-20 11:50 -0500
Re: Cannot figure out line of code, also not understanding error ApathyBear <nirchernia@gmail.com> - 2014-02-20 02:13 -0800
| From | ApathyBear <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-19 23:32 -0800 |
| Subject | Cannot figure out line of code, also not understanding error |
| Message-ID | <da9b6b96-7e9a-4094-a3f4-74cdedaf869e@googlegroups.com> |
I have two questions that come along with the following code:
----------------------------------------------------------------------
from __future__ import print_function
def sanitize(time):
if '-' in time:
splitter = '-'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
elif ':' in time:
splitter = ':'
(mins,secs) = time.split(splitter, 1)
return (mins+'.'+secs)
else:
return time
#start class
class Athlete:
def __init__(self, a_name, a_dob=None, a_times=[]):
self.name = a_name
self.dob= a_dob
self.times = a_times
def top3(self):
return(sorted(set([sanitize(t) for t in self.times]))[0:3])
#end class
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
except IOError:
print ('File error')
return (None)
james = get_coach_data('james2.txt')
print (james.name + "'s fastest times are: " + str(james.top3()))
----------------------------------------------------------------------
This is the original text file data I am working with called james2.txt located on my desktop:
James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
----------------------------------------------------------------------
1. What does this line of code mean:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.
2. Why am I getting this error when I try to run the file?
PS C:\Users\N\desktop> python gg.py
File "gg.py", line 34
except IOError:
^
SyntaxError: invalid syntax
PS C:\Users\N\desktop>
What syntax is invalid here?
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-20 19:03 +1100 |
| Message-ID | <mailman.7174.1392883425.18130.python-list@python.org> |
| In reply to | #66734 |
On Thu, Feb 20, 2014 at 6:32 PM, ApathyBear <nirchernia@gmail.com> wrote: > 1. What does this line of code mean: > return(Athlete(temp1.pop(0),temp1.pop(0), temp1) > > Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful. It's supposed to be calling Athlete() with some arguments. However, due to the extra open parenthesis between the keyword "return" and the rest, it... > 2. Why am I getting this error when I try to run the file? > PS C:\Users\N\desktop> python gg.py > File "gg.py", line 34 > except IOError: > ^ > SyntaxError: invalid syntax > PS C:\Users\N\desktop> ... causes this error, which is detected at the point where a keyword comes in that makes no sense inside the return expression. The solution is to delete the first open parenthesis: return Athlete(temp1.pop(0),temp1.pop(0), temp1) Then you have properly matched parens, and it should carry on happily. Tip: Computers report errors where they find them, which isn't always where the error actually is. But with most modern programming languages, the file is read from top to bottom and left to right, so when a problem is reported, its cause is always *before* it in the file. Never after it. You could make a horrible mess of the file after that "except" and you wouldn't change the error. (Apart from things like text encoding, which are done in a separate pass.) You'll get used to scanning up a line or two from the error to find the real cause. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Vincent Vande Vyvre <vincent.vandevyvre@swing.be> |
|---|---|
| Date | 2014-02-20 08:56 +0100 |
| Message-ID | <mailman.7175.1392883488.18130.python-list@python.org> |
| In reply to | #66734 |
Le 20/02/2014 08:32, ApathyBear a écrit :
> I have two questions that come along with the following code:
>
> ----------------------------------------------------------------------
>
> from __future__ import print_function
>
> def sanitize(time):
> if '-' in time:
> splitter = '-'
> (mins,secs) = time.split(splitter, 1)
> return (mins+'.'+secs)
> elif ':' in time:
> splitter = ':'
> (mins,secs) = time.split(splitter, 1)
> return (mins+'.'+secs)
> else:
> return time
>
>
> #start class
> class Athlete:
> def __init__(self, a_name, a_dob=None, a_times=[]):
> self.name = a_name
> self.dob= a_dob
> self.times = a_times
>
> def top3(self):
> return(sorted(set([sanitize(t) for t in self.times]))[0:3])
> #end class
>
> def get_coach_data(filename):
> try:
> with open(filename) as f:
> data = f.readline()
> temp1 = data.strip().split(',')
> return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
>
> except IOError:
> print ('File error')
> return (None)
>
> james = get_coach_data('james2.txt')
>
> print (james.name + "'s fastest times are: " + str(james.top3()))
>
>
> ----------------------------------------------------------------------
> This is the original text file data I am working with called james2.txt located on my desktop:
>
> James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
>
>
>
>
> ----------------------------------------------------------------------
>
>
>
>
>
> 1. What does this line of code mean:
> return(Athlete(temp1.pop(0),temp1.pop(0), temp1)
>
> Is it making an Athlete class? if you can give examples to help explain what this is doing that would be helpful.
>
>
>
>
> 2. Why am I getting this error when I try to run the file?
> PS C:\Users\N\desktop> python gg.py
> File "gg.py", line 34
> except IOError:
> ^
> SyntaxError: invalid syntax
> PS C:\Users\N\desktop>
>
> What syntax is invalid here?
One parenthesis missing here:
return(Athlete(temp1.pop(0),temp1.pop(0), temp1))
--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
[toc] | [prev] | [next] | [standalone]
| From | ApathyBear <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-20 00:26 -0800 |
| Message-ID | <e0d070e3-703b-4583-a845-a530b8302e1b@googlegroups.com> |
| In reply to | #66734 |
Thanks for pointing out the missing parenthesis, it makes sense now why there was an error. I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instance by assigning a class to something? like: x = Athlete(temp1.pop(0),temp1.pop(0),temp1) can a similar line look like this?: temp1.pop(0) = Athlete(temp1.pop(0),temp1)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-20 19:54 +1100 |
| Message-ID | <mailman.7176.1392886498.18130.python-list@python.org> |
| In reply to | #66737 |
On Thu, Feb 20, 2014 at 7:26 PM, ApathyBear <nirchernia@gmail.com> wrote: > Thanks for pointing out the missing parenthesis, it makes sense now why there was an error. > > I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instance by assigning a class to something? > > like: > x = Athlete(temp1.pop(0),temp1.pop(0),temp1) Calling a class will create a new instance of it. [1] What you do with it afterwards is separate. The return statement takes any value and, well, returns it. You can return anything - an Athlete instance, the integer 13423523452, the floating point value NaN, anything at all. > can a similar line look like this?: > temp1.pop(0) = Athlete(temp1.pop(0),temp1) You can't assign to a function call, so no, you can't do that. I recommend you start with the Python tutorial: http://docs.python.org/3/tutorial/index.html ChrisA [1] The class might choose to do something different (when you call bool with some argument, it'll return a pre-existing True or False), but conceptually, you still get back an instance of that class.
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gary.herron@islandtraining.com> |
|---|---|
| Date | 2014-02-20 01:04 -0800 |
| Message-ID | <mailman.7177.1392887498.18130.python-list@python.org> |
| In reply to | #66737 |
On 02/20/2014 12:26 AM, ApathyBear wrote:
> Thanks for pointing out the missing parenthesis, it makes sense now why there was an error.
>
> I suppose my question now is (and forgive my ignorance about classes, this is my first time learning them) why is it calling Athlete with some arguments? In order to make a class object, don't you need to create an instance by assigning a class to something?
>
> like:
> x = Athlete(temp1.pop(0),temp1.pop(0),temp1)
>
> can a similar line look like this?:
> temp1.pop(0) = Athlete(temp1.pop(0),temp1)
First some notation: You are not creating a class, but rather in
instance of a class. The code
class Athlete:
...
created the class, and now you are ready to create (many?) instances
of that class.
A call like
Athlete(...)
will create an instance of that class with whatever parameters you
supply. What you do with that instance after it is created is your
choice. Assignment is one possibility, but many other operation are
also possible:
x = Athlete(...)
print( Athlete(...) )
Athlete(...)+Athlete(...) # If addition made any sense and was
implemented in the class
return Athlete(...)
...
Gary Herron
[toc] | [prev] | [next] | [standalone]
| From | ApathyBear <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-20 01:22 -0800 |
| Message-ID | <ef956f04-414f-47ff-a813-bfa1152d2f29@googlegroups.com> |
| In reply to | #66734 |
On Thursday, February 20, 2014 12:54:54 AM UTC-8, Chris Angelico wrote:
>Calling a class will create a new instance of it. [1] What you do with
>it afterwards is separate.
Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.
Usually when I do something like this.
x = Athlete("Henry", "11-15-90", [1,2,3])
I can refer to things of this instance by executing x.name or whatever other attributes the class defined.
If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-20 20:41 +1100 |
| Message-ID | <mailman.7179.1392889300.18130.python-list@python.org> |
| In reply to | #66740 |
On Thu, Feb 20, 2014 at 8:22 PM, ApathyBear <nirchernia@gmail.com> wrote:
> On Thursday, February 20, 2014 12:54:54 AM UTC-8, Chris Angelico wrote:
>
>>Calling a class will create a new instance of it. [1] What you do with
>>it afterwards is separate.
>
> Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.
>
>
> Usually when I do something like this.
> x = Athlete("Henry", "11-15-90", [1,2,3])
> I can refer to things of this instance by executing x.name or whatever other attributes the class defined.
>
> If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?
Inside a method, including __init__, self refers to the object. Even
if you never assign it to anything, that instance exists somewhere,
and self can refer to it. It has an identity from the moment it begins
to exist - which is before it ever gets the name 'x' pointing to it.
You definitely should work through the tutorial I linked you to; it
explains Python's object model quite well.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-02-20 11:50 -0500 |
| Message-ID | <mailman.7197.1392914797.18130.python-list@python.org> |
| In reply to | #66740 |
ApathyBear <nirchernia@gmail.com> Wrote in message:
>
> On Thursday, February 20, 2014 12:54:54 AM UTC-8, Chris Angelico wrote:
>
>>Calling a class will create a new instance of it. [1] What you do with
>>it afterwards is separate.
>
> Okay. So what you are saying is that return(Athlete(temp1.pop(0),temp1.pop(0), temp1)) IS in fact creating an instance of Athlete. My problem with this is that there really is no declaration of 'self' for this instance.
>
>
> Usually when I do something like this.
> x = Athlete("Henry", "11-15-90", [1,2,3])
> I can refer to things of this instance by executing x.name or whatever other attributes the class defined.
>
> If I create an instance with no 'self' how does this make any sense? How would I get an attribute for the our instance above?
>
The code you're describing is inside a function:
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
temp1 = data.strip().split(',')
return Athlete(temp1.pop(0),temp1.pop(0), temp1)
So the caller might be doing something like
x = get_coach_data ("myfile.txt")
The return statement you're asking about returns a new instance of
Athlete, which gets assigned to x. Then you may try x.data or
equivalent if you like.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | ApathyBear <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-20 02:13 -0800 |
| Message-ID | <236baab2-6656-4adf-a864-76dcc7ed2c4c@googlegroups.com> |
| In reply to | #66734 |
Thank you Chris. And thank you to everyone else. This has tremendously helped me. This google group is definitely the best place for python questions/discussion. PS: Chris, I will be looking at that tutorial now.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web