The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Syntax Error in constructor?
Discuss Syntax Error in constructor? in the Python Programming forum on Dev Shed. Syntax Error in constructor? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 6th, 2013, 07:16 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 1 h 6 m 50 sec
Reputation Power: 0
|
|
|
Syntax Error in constructor?
So these past days I've been trying to get back into Python and pygame.
I've settled with Python 3.2 as that's the newest version that pygame supports.
Currently I'm trying to work my way through this tutorial:
Peter Collinridge - Pygame Physics Simulation
[Edit: I'm new so I can't post a hyperlink, but if you google that, it should be the first result]
I've gotten to part 4 without too much trouble - but cannot proceed any further as there is an issue that I don't know how to solve.
I don't think you'll need the whole code, but here it is anyway.
Code:
import pygame
import random
import math
background_colour = (255,255,255)
(width, height) = (300, 200)
class Particle():
def __init__(self, (x, y), size):
self.x = x
self.y = y
self.size = size
self.colour = (0, 0, 255)
self.thickness = 1
self.speed = 0
self.angle = 0
def display(self):
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
def move(self):
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 4')
number_of_particles = 10
my_particles = []
for n in range(number_of_particles):
size = random.randint(10, 20)
x = random.randint(size, width-size)
y = random.randint(size, height-size)
particle = Particle((x, y), size)
particle.speed = random.random()
particle.angle = random.uniform(0, math.pi*2)
my_particles.append(particle)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_colour)
for particle in my_particles:
particle.move()
particle.display()
pygame.display.flip()
[Edit: The code should be properly formatted now.]
My problem so far is on line 9:
Code:
def __init__(self, (x, y), size):
I get a syntax error on the inner parenthesis.
Now, I should say that, when last I was familiar with Python, I used v2.x - back then the above code ran fine, so there are a few changes I'll have to adjust to, I know that.
Also I usually don't ask for help, but this thing's got me stumped and all my searches have been in vain.
So, any and all help appreciated - Thanks in Advance. 
|

January 7th, 2013, 01:54 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | Originally Posted by Ironbard My problem so far is on line 9:
Code:
def __init__(self, (x, y), size):
I get a syntax error on the inner parenthesis.  |
Easy enough: change the parameters so that the location is represented by a single tuple, and unpack the tuple:
Code:
def __init__(self, loc, size):
x, y = loc
If you want to ensure the correctness of parameters, either enclose the unpacking in a try...except block or first check that “loc” is an instance of tuple.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

January 7th, 2013, 06:57 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 1 h 6 m 50 sec
Reputation Power: 0
|
|
|
: D Works like a charm! ^^ So simple, but I have to wonder - why did that change? I mean, when did that change in Python? Why is it necessary to do it like that nowadays?
|

January 7th, 2013, 09:33 AM
|
 |
Contributing User
|
|
|
|
As I recall, that's a change from python 2 to python 3. You can no longer group tuples of arguments in python3.
They made the change just when I was starting to enjoy the feature. However, you can make assignments like
Code:
$ python3
Python 3.2.3 (default, Oct 19 2012, 19:53:16)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f(*args):
... (a,*b,c,) = args # b gets everything else
... print('a',a)
... print('b',b)
... print('c',c)
...
>>> f(1,2,3,4,5,6,7)
a 1
b [2, 3, 4, 5, 6]
c 7
>>>
__________________
[code] Code tags[/code] are essential for python code!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|