Accessing class attribute
Dave Angel
davea at davea.name
Thu Sep 12 06:23:45 EDT 2013
On 12/9/2013 02:15, chandan kumar wrote:
> Hi ,
>
> I'm new to python
Welcome. I hope you enjoy your time here, and that the language treats
you as well as it's treated me.
> ,please correct me if there is any thing wrong with the way accessing
class attributes.
None of the following uses class attributes, but instead uses instance
attributes. For example, any attri bute created with self.xxxx =
inside an instance method will bei an instance attribute. Class
attributes are typically created by assignments outside any method.
>
> Please see the below code .I have inherited confid in ExpectId class, changed self.print_msg to Hello. Now inherited confid in TestprintmsgID class.Now I wanted to print self.print_msg value (which is changed under ExpectId class) as Hello under TestprintmsgID. I end up with error saying TestprintmsgID has no attribute self.print_msg. Atleast i expect the null to be printed.
Please include the full error message, (including the traceback), rather
than just paraphrasing. In this case, it's easy to guess, but
frequently it is not.
>
>
>
> class confid():
In Python 2, you always want to derive from object. That's to signal
the compiler to use "new style" classes, which have been the preferred
way for many years. Python 3 only has new-style classes.
class confid(object):
> def __init__(self):
> self.print_msg = ""
>
> class ExpectId(confid):
>
> def __init__(self):
> self.print_msg = " Hello"
>
> def expectmethod(self):
> print "self.print_mesg = ",self.print_msg
>
Each instance of ExpectId is also an instance of confid.
> class TestprintmsgID(confid):
>
> def __init__(self):
> "Created Instance"
>
> def printmsgmethod(self):
> print "printmsgmethod print_msg val = ",self.print_msg---- Here is the Attribute error
>
>
Each instance of TestprintmsgID is also an instance of confid, but NOT
of ExpectID.
The base class methods are not
automatically called when the child class methods are. if you want them
called, you have to do it explicitly. In particular, you should always
call the base class __init__() method from your __init__() method. It
didn't happen to matter in the case of ExpectID, but it does here.
Preferred way to do that is to add a line to iTextprintmsgID's
__init__() method:
super(ExpectId, self).__init__()
should appear at the beginning of your initializer. This will eliminate
your Attribute error. But it will still see a value for print_msg of
bank string.
> if __name__ == '__main__':
> ins1 =ExpectId()
> ins1.expectmethod()
>
> ins2 = TestprintmsgID()
> ins2.printmsgmethod()
>
I think you're expecting that deriving from a class somehow patches the
existing class. But it doesn't. The existence of the ExpectedID class
only affects instances of that class, not instances of TestprintmsgID.
If you wanted those to be affected, you'd be needing to inherit from
ExpectID, rather than just from confid. If you did that, you'd see a
value of "Hello".
--
DaveA
More information about the Python-list
mailing list