473,396 Members | 1,966 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,396 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 2081
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
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.