473,226 Members | 1,375 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,226 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 14155
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

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

Similar topics

2
by: Hayden Kirk | last post by:
Hey Is it possible to do without $this? I have got an instance of another class inside a class. Now I want to access this classes functions using the self keyword, but it seems i cant. Or...
37
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
2
by: Midas | last post by:
Greetings everyone, I'm including code, for cut/paste, to better explain my question. I create a Car object then I call some of its methods. No problem. Then I try to pass, to a function, the name...
14
by: bruce stockwell | last post by:
Using 'self' in classes seems pretty straight forward. My curiosity is why I have to use it. Shouldn't it be implied? If I create an instance of 'human' called 'bruce' and call the method 'blink'...
1
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the example is here :...
14
by: Tieche Bruce A MSgt USMTM/AFD | last post by:
I am new to python, Could someone explain (in English) how and when to use self? I have been reading, and haven't found a good example/explanation
13
by: ateale | last post by:
Hey Guys I know I am missing some important points on how to use the python language. Is it possible to pass variables between methods (is that what theyare called?) in my attempt below I have...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.