473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

print is giving namespace information?

TMS
119 New Member
At least I think thats what it is doing. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. class Particle:
  2.     def __init__(self, symbol, charge, number):
  3.         self.symbol = symbol
  4.         self.charge = charge
  5.         self.number = number
  6. class Nuclear:
  7. def __repr__(self):
  8.         return self.symbol
  9. class Nuclear(Particle):
  10.     def __repr__(self):
  11.         return "(%d)%s" % (self.number, self.symbol)
  12.  
  13. if __name__ == '__main__':
  14.     em = Particle("e-", -1, 0)
  15.     print em
  16.  
This is what I get when I run it:
<__main__.Parti cle instance at 0x00C42468> which is the address of where this is located in the namespace I think.

What I'm after is for print em to give me e-

The code was written by the teacher (whom I should be asking, I know) but I want to get an early start on this assignment because when this is figured out I have to write a 'nuclear reactor' program (yipes). So, I'm hoping one of you crazy smart python programmers out there will see what I've done wrong, because I've tried many many things and I can't figure it out. Thank you for your help!
Feb 22 '07 #1
5 1322
TMS
119 New Member
I think I just figured it out... I've defined Nuclear twice.


nvm
Feb 22 '07 #2
bvdet
2,851 Recognized Expert Moderator Specialist
At least I think thats what it is doing. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. class Particle:
  2.     def __init__(self, symbol, charge, number):
  3.         self.symbol = symbol
  4.         self.charge = charge
  5.         self.number = number
  6. class Nuclear:
  7. def __repr__(self):
  8.         return self.symbol
  9. class Nuclear(Particle):
  10.     def __repr__(self):
  11.         return "(%d)%s" % (self.number, self.symbol)
  12.  
  13. if __name__ == '__main__':
  14.     em = Particle("e-", -1, 0)
  15.     print em
  16.  
This is what I get when I run it:
<__main__.Parti cle instance at 0x00C42468> which is the address of where this is located in the namespace I think.

What I'm after is for print em to give me e-

The code was written by the teacher (whom I should be asking, I know) but I want to get an early start on this assignment because when this is figured out I have to write a 'nuclear reactor' program (yipes). So, I'm hoping one of you crazy smart python programmers out there will see what I've done wrong, because I've tried many many things and I can't figure it out. Thank you for your help!
TMS,

It looks like you are coming along very well. Let us know if you need some help, or show us some of your stuff when you get further along.

One thing:
Expand|Select|Wrap|Line Numbers
  1. if __name__ == '__main__':
  2.     em = Nuclear("e-", -1, 0)
  3.     print em
  4.  
Feb 22 '07 #3
TMS
119 New Member
Well... we've been going through Python at breakneck speed. I feel as though my face is in a permanent G6 (you know, pressed back lips, cheeks, hair pulling out the back of my head from the speed), yet I think I'm getting to understand Python a little better each day.

Right now we are building a Nuclear Reactor - Or sort of. The first part works fine. There are 3 more parts of it that I have to accomplish and I'm already stuck!

The first of the 3 parts includs a new class which is created with a left hand side and a right hand side, both specified by tuples containing particles. The reaction will transform the particles on the left hand side to the particles on the right hand side. (lhs = left hand side, rhs = right hand side).

The problem I'm having is I can't get it to print out the way I need it to. I see that the new class inherits from the Particle class and I think it goes like this:

Expand|Select|Wrap|Line Numbers
  1. class Nucleus(Particle):
  2.     def __repr__(self):
  3.         return "(%d)%s" % (self.number, self.symbol)
  4.  
  5. class UnbalancedCharge:
  6.     pass #worry about this later
  7.  
  8. class UnbalancedNumber:
  9.     pass #worry about this later
  10.  
  11. class react(Nucleus):     #this is the new class
  12.     def __init__(self, lhs, rhs):
  13.         self.lhs = lhs
  14.         self.rhs = rhs
  15.     def __repr__(self):
  16.         return Nucleus(lhs, lhs), "->" , Nucleus(rhs, rhs) #this can't be right
  17.  
What should happen is when requested to
print react((li6, d), (he4, he4))

it should return this:
(6)Li + (2)H -> (4)He + (4)He

The class Nucleus prints the (6)Li and (2)H as well as the (4)He parts. BUT, when inheriting from that class, I should be able to use the format from Nucleus class to print out the new format right?

Should I use the init from the Nucleus class or do it like I have it? Eventually the lhs will be compared to the rhs to see if they are equal.

I'm hoping for a nudge in the right direction...
TMS
Feb 22 '07 #4
bvdet
2,851 Recognized Expert Moderator Specialist
This will not work, but it may be closer:
Expand|Select|Wrap|Line Numbers
  1. class react(Nucleus):     #this is the new class
  2.     def __init__(self, lhs, rhs):
  3.         self.lhs = lhs
  4.         self.rhs = rhs
  5.     def __repr__(self):
  6.         return '%s->%s' % (repr(Nucleus(lhs, lhs)),  repr(Nucleus(rhs, rhs)))    
Feb 22 '07 #5
TMS
119 New Member
OK, maybe I'm thinking about this all wrong. Do I really need another __init__? Couldn't I write some functions defining the rhs and lhs? I've been trying some other things, but I can't figure it out. Time to go to a different class.
Feb 22 '07 #6

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

Similar topics

3
3057
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
1
2084
by: JSB | last post by:
I am new to Access and am hoping an Access guru out there can help me. I am using an Access 2002 project to report on a SQL Server database. I am reporting various information for around 100 different Clinics that are affiliated with our hospital. Each Clinic is supposed to get its own report, named "Clinic ABC - FY2004 Q4.pdf". I would like to push one button and have all of the pdf files created. Instead, I am running the report 100...
9
1686
by: Rhino | last post by:
How hard (and desireable) would it be to give the user certain choices when it comes to printing web pages? The pages on my site use colours and pictures and contain an imbedded menu, among other things. Now, I could make certain assumptions on the user's behalf, such as making the background white and the text black, suppressing the menu from the printed page, and even suppressing the printing of the pictures if they are merely...
5
1912
by: Tony | last post by:
Every 10 seconds I need to search a SQL table for orders to print. The orders are created through WebForms on ASP.NET clients. The orders should not print until 10 minutes before they are due. The network printer will be available from the server running IIS and SQL Server 2000. If this were a Windows Forms application, then I would create a system timer in Form1.Load(). How is this done in an ASP.NET application?
1
2371
by: melanieab | last post by:
Hi, I have a form with tab pages on it, and I'd like to print each page whenever a button is pressed (all the tabpages share the same print button - I just give it a different parent when another tab is pressed). But I can only print once. When the print button is pressed the second time, I get error An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll Additional information:...
15
22863
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which could either be f1() or f2(). BRs!
12
3541
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to print the report three times, but do not know how
16
4526
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the screen, when I open a file, the entire contents of the file is in fact being shown...so why can't I print it later? All of this code I am getting from a book (Chris Sells) and the net. The solution is to be found in the fact that stringbuilder is...
2
7994
by: RB0135 | last post by:
Hi All, Is this possible: 1) An application prints to a printer/print queue (mainly XP). 2) A vb.net application monitors the print queue (I know this can be done) 3) Retrieve the RAW information from the PrintJob (Cant seem to find anything on this) 4) Delete the Job when I have the RAW information (I know this can be done)
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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 we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.