473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

_init() not called automatically , when an object is made.

Pls refer to the given below program:
Expand|Select|Wrap|Line Numbers
  1. class t2:
  2.     a=0
  3.     def _init_(self):
  4.         t2.a=10
  5.     def f1(self,temp):
  6.         self.a=temp
  7.     def f2(self):
  8.         print self.a
  9. O1=t2()
  10. O1.f2()
  11. O1.f1(1000)
  12. O1.f2()
  13. O1.a=2000
  14. O1.f2()
The result is :
0
1000
2000
And when called explicitly :
Expand|Select|Wrap|Line Numbers
  1. class t2:
  2.     a=0
  3.     def _init_(self):
  4.         t2.a=10
  5.     def f1(self,temp):
  6.         self.a=temp
  7.     def f2(self):
  8.         print self.a
  9. O1=t2()
  10. O1._init_()
  11. O1.f2()
  12. O1.f1(1000)
  13. O1.f2()
  14. O1.a=2000
  15. O1.f2()
gives us
10
1000
2000
Aug 5 '14 #1
3 2082
bvdet
2,851 Expert Mod 2GB
You have defined method "__init__" as "_init_". "__init__" (2 leading and trailing underscores) will be called when an instance is created, but not "_init_".
Aug 5 '14 #2
dwblas
626 Expert 512MB
Also you are using both class and instance attributes named "a", which are not the same.
Expand|Select|Wrap|Line Numbers
  1. class T2:
  2.      a=0
  3.      def __init__(self):
  4.          T2.a=10
  5.      def f1(self,temp):
  6.          self.a=temp
  7.      def f2(self):
  8.          print self.a
  9.  
  10. class T3:
  11.     def __init__(self):
  12.         T2.a=20
  13.  
  14. O1=T2()
  15. O1.f2()
  16. O1.f1(1000)
  17. O1.f2()
  18. O1.a=2000
  19. O1.f2()
  20.  
  21. print T2.a
  22. third = T3()
  23. print T2.a 
Aug 5 '14 #3
Verified.
Thanks for the guidance.
Aug 19 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Sriek | last post by:
hi, i come from a c++ background. i ws happy to find myself on quite familiar grounds with Python. But, what surprised me was the fact that the __init__(), which is said to be the equivlent of the...
8
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean...
6
by: Saurabh | last post by:
Hi All, I want to know something. Apart from Constructor and Destructor are there any other functions which get called automatically without user calling them? If yes,please tell me which are...
3
by: Academic | last post by:
If I do the following: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor Do I need to reset the cursor or will that happen automatically when I leave the subroutine?
5
by: questionit | last post by:
Hi I am finding it difficult to use Form_Current() event of a subform. It gets called automatically. I changed the tab order so that the Sub Form with Form_Current() event came first or in the...
16
by: Greg (codepug | last post by:
If one converts that .mdb into an .mde the code is secure but the tables can still be imported. Just for Very Basic protection, I have placed a Password on the database using the "Set Database...
3
by: senthilkumarb | last post by:
How can i call a function automatically when the server started ?
17
by: handique | last post by:
Hi, I have javascript code for rotating images, but the rotation starts only when mouse is placed over the image. But i want to rotate images automatically when the page loads. Can any guide me in...
4
by: sadsan | last post by:
Hi everyone I'm after some advice and i don't know if this has already been developed or not. I'm wanting to be able to start a program automatically when its drop into a folder specifically a...
1
by: sailoosha | last post by:
Hi, I am getting an error "operation not allowed when object is closed" when executing the following code. I am getting the runtime error at While Recordset.EOF <> True I am not sure where the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.