Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Two quick questions (http://www.programmingforums.org/showthread.php?t=10171)

bulio Jun 4th, 2006 10:47 AM

Two quick questions
 
Hey everyone,

I'm currently learning Python, and I have two quick questions.

The first one is, when defining an object inside of a class why do we use (self)?

ie:

:


class Person
    def bulio(self):
        print "Bulio says hi." #Use self so it will do this?


Is it because self will make the object run what is inside of it? (And therefore use the print?)

Secondly, would it be wise for a Python newbie to start learning pygame right away? I know my basic loops, if statements, functions, and classes.

Thanks!

megamind5005 Jun 4th, 2006 10:59 AM

The only way you're gonna find out whether it's wise or not is to actually look at some example code and read/do some starters' tutorials. If you've learnt enough to be asking about objects and definition, I don't think you're far from reaching the point where you'll want to/should learn things like PyGame, PyMedia, BeautifulSoup etc.

Get some source code for the simpler games like Tetris from the PyGame website and look at them. That's how I kinda started ;)

Arevos Jun 4th, 2006 11:14 AM

Quote:

Originally Posted by bulio
Is it because self will make the object run what is inside of it? (And therefore use the print?)

I'm not quite sure what you mean here.

In Python, the first argument of a method belonging to a function is a reference to the object the function is contained within. This argument is, by convention, called "self".

Thus:
:

x = MyClass()
x.foobar()

Is the same as:
:

x = MyClass()
MyClass.foobar(x)

The "self" argument allows a method to access the object in which it is contained.

e.g.:
:

class Test:
    def __init__(self):
        self.x = 10
        self.y = 5

t = Test()
print t.x, t.y

Prints out:
:

10 5

bulio Jun 4th, 2006 11:36 AM

Thanks guys, I think I understand it now.


All times are GMT -5. The time now is 4:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC