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 November 11th, 2012, 03:08 PM
Taylorhill89 Taylorhill89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 Taylorhill89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 28 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old November 11th, 2012, 06:57 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Code:
class Super:

    def __init__(self):
        '''
            self is the name of the variable assigned the new Super object
        '''
        a = 'THIS VARIABLE IS LOCAL TO THE __init__ FUNCTION' # a is inaccessible after the function finishes.
        # (or at least it's hard to find)
        self.b='THIS ASSIGNS AN ATTRIBUTE OF A Super OBJECT'

class Sub(Super):

    def __init__(x):
        '''
            self is common but not reserved nor special.
            Any variable name will do.
            (At least you'll have trouble finding an exception to that statement.)
        '''
        Super.__init__(x)
        c = 'THIS VARIABLE IS LOCAL TO THE __init__ FUNCTION'
        x.d = 'THIS ASSIGNS AN ATTRIBUTE OF A Sub OBJECT'

abcd = Sub()

print('abcd.b is %s'%abcd.b)
print('abcd.d is %s'%abcd.d)
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old November 12th, 2012, 11:40 AM
Taylorhill89 Taylorhill89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 Taylorhill89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 28 sec
Reputation Power: 0
Ahh-- this makes sense now. Thank you for the help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Scope of Class Variables and Methods

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