Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 4th, 2006, 10:47 AM   #1
bulio
Hobbyist Programmer
 
bulio's Avatar
 
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5 bulio is on a distinguished road
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!
bulio is offline   Reply With Quote
Old Jun 4th, 2006, 10:59 AM   #2
megamind5005
Programmer
 
megamind5005's Avatar
 
Join Date: Dec 2004
Location: UK
Posts: 53
Rep Power: 4 megamind5005 is on a distinguished road
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
__________________
Tetris is so unrealistic
megamind5005 is offline   Reply With Quote
Old Jun 4th, 2006, 11:14 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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
Arevos is offline   Reply With Quote
Old Jun 4th, 2006, 11:36 AM   #4
bulio
Hobbyist Programmer
 
bulio's Avatar
 
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5 bulio is on a distinguished road
Thanks guys, I think I understand it now.
bulio is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 PM.

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