471,330 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Passing self as argumnet to other methods of a class

440 256MB
Hi,

I have seen some snippets of the code where the 'self' keyword is passed as a argument to method.Below is the sample code.Could anybody suggest whether it is the right approach to do in Python.

Expand|Select|Wrap|Line Numbers
  1. Sample.py
  2. class Sample1:
  3.     def __init__(self):
  4.         self.Y = 20.0
  5.         self.Z = 30.0
  6.  
  7.     def Func1(self):
  8.         print 'Func1'
  9.         objSample2 = Sample2()
  10.         objSample2.Func2(10.0,self)
  11.  
  12. class Sample2:
  13.     def Func2(self,X,obj):
  14.         print 'Func2'
  15.         print X
  16.         print obj.Y
  17.         print obj.Z
  18.  
  19.  
  20. if __name__ == '__main__':
  21.     objSample1 = Sample1()
  22.     objSample1.Func1()
  23.  
Aug 20 '07 #1
1 12994
Hi,

I have seen some snippets of the code where the 'self' keyword is passed as a argument to method.Below is the sample code.Could anybody suggest whether it is the right approach to do in Python.

Expand|Select|Wrap|Line Numbers
  1. Sample.py
  2. class Sample1:
  3.     def __init__(self):
  4.         self.Y = 20.0
  5.         self.Z = 30.0
  6.  
  7.     def Func1(self):
  8.         print 'Func1'
  9.         objSample2 = Sample2()
  10.         objSample2.Func2(10.0,self)
  11.  
  12. class Sample2:
  13.     def Func2(self,X,obj):
  14.         print 'Func2'
  15.         print X
  16.         print obj.Y
  17.         print obj.Z
  18.  
  19.  
  20. if __name__ == '__main__':
  21.     objSample1 = Sample1()
  22.     objSample1.Func1()
  23.  

Yes, this is the right way (as in, it all works). This is actually similar to what I used when learning about classes. More expert users can give you more examples, but for now it should suffice that your on the right path.
Aug 21 '07 #2

Post your reply

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

Similar topics

2 posts views Thread by Hayden Kirk | last post: by
37 posts views Thread by Grzegorz Staniak | last post: by
14 posts views Thread by bruce stockwell | last post: by
1 post views Thread by Alexander Kervero | last post: by
14 posts views Thread by Tieche Bruce A MSgt USMTM/AFD | last post: by
13 posts views Thread by Kurda Yon | last post: by
8 posts views Thread by ssecorp | last post: by
reply views Thread by rosydwin | last post: by

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.