Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2012, 08:43 PM   PM User | #1
Tay
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Tay is an unknown quantity at this point
Python: Scope of class variables and methods.

I have a question about class variables and scope in Python.

Here is the code:
######
class Super:
def __init__(self):
a = "this is b, the b of Super without self"
self.b="this is b, the self.b of Super"

class Sub(Super):
def __init__(self):
Super.__init__(self)
c = "this is c, of sub, without self"
self.d = "this is d, self.d of Sub"

abcd = Sub()
######

Why wouldn't I be able to type abcd.a and get the value of a? I must be misunderstanding something--isn't the point of the __init__ function in the superclass to initialize variables in the subclasses, so that they can be used without having to define them in every class using self?
Tay is offline   Reply With Quote
Old 11-12-2012, 03:43 PM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by Tay View Post
I have a question about class variables and scope in Python.

Here is the code:
######
Code:
class Super:
    def __init__(self):
        a = "this is b, the b of Super without self"
        self.b="this is b, the self.b of Super"

class Sub(Super):
    def __init__(self):
        Super.__init__(self)
        c = "this is c, of sub, without self"
        self.d = "this is d, self.d of Sub"

abcd = Sub()
######

Why wouldn't I be able to type abcd.a and get the value of a? I must be misunderstanding something--isn't the point of the __init__ function in the superclass to initialize variables in the subclasses, so that they can be used without having to define them in every class using self?
Reposted for you, using the code tags to preserve formatting - important in python
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 03:56 PM   PM User | #3
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Right. Had a look, and there's a very valid reason why you won't be able to get .a, but retrieve .b.

The self keyword passes the class object into the initiation function. So, to assign a property to the class you need to use self.property name.

In other words, using a on it's own keeps it in the function scope, but using self.a assigns a property to the class object.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Old 11-12-2012, 05:40 PM   PM User | #4
Tay
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Tay is an unknown quantity at this point
Thanks so much for the help. I knew there were some simple rules that I wasn't quite understanding. Somehow I couldn't get that from searching.
Tay is offline   Reply With Quote
Old 11-12-2012, 06:35 PM   PM User | #5
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by Tay View Post
Thanks so much for the help. I knew there were some simple rules that I wasn't quite understanding. Somehow I couldn't get that from searching.
Not a problem . Scope is a tricky subject when you're learning, sometimes.
__________________
Useful function to retrieve difference in times
The best PHP resource
A good PHP FAQ
PLEASE remember to wrap your code in [PHP] tags.
PHP Code:
// Replace this
if(isset($_POST['submitButton']))
// With this
if(!empty($_POST))
// Then check for values/forms. Some IE versions don't send the submit button 
Quote:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
BluePanther is offline   Reply With Quote
Reply

Bookmarks

Tags
python

Jump To Top of Thread


Thread Tools
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

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

Forum Jump


All times are GMT +1. The time now is 11:53 AM.


Advertisement
Log in to turn off these ads.