import pygame
from pygame.locals import *
from sys import exit

from random import *

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
            
    screen.lock()
    
    for count in range(10):
        random_color = (randint(0, 255), randint(0, 255), randint(0, 255))
        random_pos = (randint(0, 639), randint(0, 479))
        random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1], 479))
        
        pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
    screen.unlock()
    
    pygame.display.update()

In the above code, why are the three lines following the second for loop encased in parentheses? And what is the last of those three lines.(639-randint? How did they just slap a number and a dash right in front of a function. Thanks for any and all replies.

On a side note, why doesn't python support function, method, and constructor overloading? And what does it do to compensate?

Those parenthesis are there because tuples are defined that way, even many times it is ok to leave them out, the comma is enough. What you mean not support overloading, what is your problem? That dash is called minus and for example 3-1==2

Edited 6 Years Ago by pyTony: n/a

1) You are using those variables to be passed into a function (pygame.draw.rect). This function expects the variables to be in a tuple/list.
2) 639 is one less then the width of the screen. It will make sure that you are not drawing off the screen.
The dash is the subtraction.
The function is called and returns a number. So instead of '639 - fuction' it is, '639 - returned_variable'

The side notes: I have no clue. This is my 2nd programming language.

In the above code, why are the three lines following the second for loop encased in parentheses? And what is the last of those three lines.(639-randint? How did they just slap a number and a dash right in front of a function. Thanks for any and all replies.

Why dont you test it out?

>>> from random import * #bad way to import
>>> from random import randint
>>> random_color = (randint(0, 255), randint(0, 255), randint(0, 255))
>>> random_color
(164, 184, 42)
>>> repr(random_color)
'(164, 184, 42)'
>>> random_color = randint(0, 255), randint(0, 255), randint(0, 255)
>>> random_color
(130, 206, 142)
>>> repr(random_color)
'(130, 206, 142)'
>>> #As you see the same

On a side note, why doesn't python support function, method, and constructor overloading? And what does it do to compensate?

Python does not support function overloading but compensate well by use off default arguments and keyword arguments.
There are also rather ugly global statement,that we dont talk about.

def foo(n=None):
    if n is None: n = 10
    return n + 20

print foo()
#-->30

If we talk about a class.
The constructor for a Python class is def __init__(self, ...): and you cannot overload it.
What you can do is use defaults for the arguments.

class Car:
    def __init__(self, carName="Bmw", Color='red', Speed=250):
        self.name = carName
        self.color = Color
        self.speed = Speed

        self.printStats()      

    def printStats(self):
        print 'Car Statistics:'
        print "Name      : %s" % self.name
        print "Color     : %s" % self.color
        print "Speed:    : %s" % self.speed
        print "----------------------------"
        
car_1 = Car()
car_2 = Car('Opel', 'Black', 220)

'''My output-->
Car Statistics:
Name      : Bmw
Color     : red
Speed:    : 250
----------------------------
Car Statistics:
Name      : Opel
Color     : Black
Speed:    : 220
----------------------------
'''

Edited 6 Years Ago by snippsat: n/a

Thanks everyone. Snippsat, what if you want them with a different number or arguments?

Thanks everyone. Snippsat, what if you want them with a different number or arguments?

Use the power off python list,dict,set as default argument.
The link tony show the use off *arg(tuple) **arg(dictionary)
Python has much power up it`s sleeves.
Maybe simpler to understand it like this.

def foo(*arg):
    '''Return a tuple'''
    print arg
    
foo(1,2,3,4,5,'test')
foo()


def foo1(**arg):
    '''Return a dictionary'''
    print arg

foo1(a='1', b='2')
foo1()

'''Out-->
(1, 2, 3, 4, 5, 'test')
()
{'a': '1', 'b': '2'}
{}
'''
class Car:
    def __init__(self, carName=['Bmw'], Color=['red'] , Speed=[250]):
        self.name = carName
        self.color = Color
        self.speed = Speed

        self.printStats()

    def printStats(self):
        print 'Car Statistics:'
        print "Name      : %s" % self.name
        print "Color     : %s" % self.color
        print "Speed:    : %s" % self.speed
        print "----------------------------"

car_1 = Car()
car_2 = Car(['fiat','opel','ferrari'], ['black','Blue','Yellow'], [200,220,300])

'''Out-->
Car Statistics:
Name      : ['Bmw']
Color     : ['red']
Speed:    : [250]
----------------------------
Car Statistics:
Name      : ['fiat', 'opel', 'ferrari']
Color     : ['black', 'Blue', 'Yellow']
Speed:    : [200, 220, 300]
----------------------------
'''

Edited 6 Years Ago by snippsat: n/a

Thanks for simple examples, in real use I would rethink though the passing or arguments in this case:
car_2 = Car(, , [200,220,300])

I would prefer:
car_2 = Cars(('fiat','black',200),('opel','blue',220),('ferrari','yellow',300))

and do appropriate for over *args in init (changed the object name to end with s to reflect possible sequence arguments)

Edited 6 Years Ago by pyTony: n/a

This article has been dead for over six months. Start a new discussion instead.