473,507 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call a function within a class from another class

Hi there,

could you help me spot my error - I'm sure it's a simple one. I'm just trying to understand classes in Python a little better (working through Learn Python The Hard Way).

Basically I want the printokay function in Testclass2 to run after being called from Testclass1

Expand|Select|Wrap|Line Numbers
  1. class Testclass1(object):
  2.  
  3.     def __init__(self, var1):
  4.         self.printthis = var1
  5.  
  6.     def printthing(self):
  7.         lookatme = self.printthis
  8.         Testclass2(lookatme).printokay()
  9.  
  10. class Testclass2(object):
  11.     def __init(self, var2):
  12.         self.okayletsprint = var2
  13.  
  14.     def printokay(self):
  15.         print "this is text"
  16.         print self.okayletsprint
  17.  
  18. a = Testclass1("jumbo")
  19. a.printthing()
  20.  
Nov 13 '10 #1
4 44141
bvdet
2,851 Recognized Expert Moderator Specialist
You left out 2 underscore characters in Testclass2.__init__(self).
Nov 13 '10 #2
okay, underscores are included - thanks for spotting that.

I've been changing it around and now when I run this it still doesn't print (no error messages - nada).

Expand|Select|Wrap|Line Numbers
  1. class Testclass1(object):
  2.  
  3.     def __init__(self, var1):
  4.         self.printthis = var1
  5.  
  6.     def printthing(self):
  7.         lookatme = self.printthis
  8.         Testclass2.printokay(lookatme)
  9.  
  10. class Testclass2(object):
  11.     def __init__(self, var2):
  12.         self.okayletsprint = var2
  13.  
  14.     def printokay(self):
  15.         print "this is text"
  16.         print self.okayletsprint
  17.  
  18. a = "jumbo"
  19. Testclass1(a).printthing
  20.  
Nov 13 '10 #3
bvdet
2,851 Recognized Expert Moderator Specialist
In order to call function or a method of an instance, you must include the parentheses. Testclass1(a).printthing()
Nov 13 '10 #4
Excellent, thank you. Ammended code is attached for anyone that has a similar problem.

Expand|Select|Wrap|Line Numbers
  1. class Testclass1(object):
  2.  
  3.     def __init__(self, var1):
  4.         self.printthis = var1
  5.  
  6.     def printthing(self):
  7.         Testclass2().printokay()
  8.  
  9. class Testclass2(object):
  10.     def __init__(self):
  11.         self.okayletsprint = 1
  12.  
  13.     def printokay(self):
  14.         print "this is text"
  15.         print self.okayletsprint
  16.  
  17.  
  18. a = Testclass1("jumbo bitch")
  19. a.printthing()
  20.  
  21.  
Nov 13 '10 #5

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

Similar topics

2
2025
by: AJ | last post by:
Ok, I have a "form" class, which I am going to use to generate HTML forms. I also have a form_input, which I use to generate the individual HTML input elements. What I want to do is some...
2
3439
by: Dave Smithz | last post by:
Hello, Is there a way of associating what colour the hyperlinks should be within a particular class? Let me elaborate: I am using programming to generate HTML code. I want different...
11
1662
by: Bob Rock | last post by:
Hello, I'd like to be able to allow instanciation of a class (class Class_A) only from another class method (class Class_B). And I'd like to write the necessary code to enforce this behavior...
3
4828
by: | last post by:
This is a semi-advanced question about ASP VBScript 5.0 classes. If you're knowledegable, please lend a hand! VBScript class instances can have properties that have objects assigned to them....
2
3358
by: John J | last post by:
I have written the following overload of operator << as a display function. In the code I unsuccessfully try and call a function within another class(<< "Race : " << r->Show () << endl). The Show...
4
3530
by: Art | last post by:
Hi, I have a main form for my project. It uses a routine in another class to do work on a file -- line by line. I'd like to have a text box on my form display the lines, say every 5,000 -- so...
5
1937
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
3
8618
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
2
2648
by: AboutJAV | last post by:
I created a mainmenu in the form class. I added the following code public delegate void ChangeMainMenuDelegate(bool bState); public void ChangeMainMenuState(bool bState) { for (int n = 0; n <...
16
4134
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
7223
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
7319
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
7376
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
7485
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
5623
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,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.