Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 2nd, 2003, 08:14 AM
HarryF's Avatar
HarryF HarryF is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 14 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Declaring Python class variables

Playing around with Python for the first time and have a question about classes - how to you declare class variables? I know you don't have to but I'd like to to make the class easier to read.

In PHP, for example, I might do this;

Code:
class Test {
    var $message; // declare variable
    function Test ($message) {
        $this->message=$message;
    }

    function get() {
        return $this->message;
    }
}


In Python I've got as far as;

Code:
class Test:
    def __init__(self,message):
        self.message=message

    def get(self):
        return self.message


I'd like to be able to declare the message variable to make it readable.

One other question; when declaring class methods, do I always have to make the first parameter "self"? And why is it Python does this in the first place? Seems a little odd compared to methods in Java or PHP for example.

Many thanks.

Reply With Quote
  #2  
Old April 5th, 2003, 10:05 AM
OB_redemption's Avatar
OB_redemption OB_redemption is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Posts: 32 OB_redemption User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
>> how to you declare class variables?

You mean instance variables (data members)? Like this:
Code:
class Test:
    my_attr = '';
    def __init__(self,message):
        self.message=message

    def get(self):
        return self.message

Or you can just use self.message like you showed and the instance variable will spring into existence.

To define private variables, add 2 leading underscores (e.g. __myvar) and Python will mangle it into _classname__myvar. Data hiding in this case is not strictly enforced as in Java (since you can access it using '_classname__myvar' - it's more of a 'contractual agreement' basis)

>> when declaring class methods, do I always have to make the first parameter "self"? And why is it Python does this in the first place?

Consider an object x of class Test (which you defined above). Typically, from outside the class, you'd call the get() method of x like so:
Code:
msg = x.get();

What Python does is translate it into
Code:
msg = Test.get(x);

Why the parameter is called 'self' is a matter of convention (meaning you can use something like 'pinkgoblin' instead of 'self', but it's good to follow the convention to prevent confusing others)

Reply With Quote
  #3  
Old April 6th, 2003, 08:53 AM
HarryF's Avatar
HarryF HarryF is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 14 HarryF User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Many thanks. Clears up alot.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Declaring Python class variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap