473,387 Members | 1,624 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,387 software developers and data experts.

objects not working right: attribute error

I am completely stuck and am looking for guidance. I am creating my own version of the sets class, making the class called Set. I have created a test function as well. Everything works great until I get to the union/subtract part.
Expand|Select|Wrap|Line Numbers
  1. def intersection(self,set2):
  2.         self.inter=Set([])
  3.         for i in self.elements:
  4.             for j in set2.elements:
  5.                 if i == j:
  6.                     self.inter = self.inter.addElement(j)
  7.         return self.inter
  8.  
  9.     def union(self,set2):
  10.         self.un=Set([])
  11.         for i in self.elements:
  12.             self.un = self.un.addElement(i)
  13.             for j in set2.elements:
  14.                 if i!=j:
  15.                     self.un = self.un.addElement(j)
  16.         return self.un
  17.  
  18.     def subtract(self,set2):
  19.         self.sub=Set([])
  20.         for i in self.elements:
  21.             self.sub= self.sub.addElement(i)
  22.             for j in set2.elements:
  23.                 if i==j:
  24.                     self.sub= self.sub.deleteElement(i)
  25.         return self.sub
  26.  
Now, intersection works fine. I basically was following the same pattern for union, but when I run it...

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 87, in <module>
  3.     main()
  4.   File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 83, in main
  5.     print "Union:", set1.union(set2)
  6.   File "C:\Users\monty27\Desktop\Math Computing\setsMontgomery.py", line 39, in union
  7.     self.un = self.un.addElement(j)
  8. AttributeError: 'list' object has no attribute 'addElement'
  9.  
Thoughts? I've messed around with this quite a bit, trying to just copy self.elements and then verbatim use the intersection() code with the exception of making the if statement i!=j. It just does not work.
Nov 24 '11 #1
1 1940
Glenton
391 Expert 256MB
Hi

This may or may not help, but the logic is wrong for intersection. You're adding all the elements of set2 multiple times. Basically for each i you add all the elements of set2 that are not equal to i (which is all of them or all of them except 1).

This is I suppose an issue since you appear to be going to quite a bit of effort to ensure that addElement only adds new elements.

If this is indeed the issue, I would consider making addElement itself more robust. So when you add an element, check if it's in there already and only add it if not.

Let me know if that helps. If not, please post the addElement method.
Nov 24 '11 #2

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

Similar topics

3
by: Amy Kimber | last post by:
Hello, I have a file upload page, and I've had it working fine, it was beautiful :-) Anyway, the powers that be moved hosts... and it doesn't work now. The file name is correct, the directory...
9
by: M.N.A.Smadi | last post by:
HI; I am having the following error. I am using someone else's code and all they are doing is pass an argv to a function then def execute_action(manager, argv): method_name =...
1
by: Bonavox | last post by:
Hi! FYI: I had some trouble with the type attribute in one of my providers in web.config. I was writing it by hand, and got this error all the time: Required attribute 'type' not found....
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
1
by: n4ixt | last post by:
I have two macros I used to use in VS 2003. I recently tried to import them for use in VS 2005, but they don't seem to work. I open the macro explorer, right click and do run, but it's like VS...
3
by: Teja | last post by:
Hi all, What is attribute error? what causes that error, especially with COM objects? To be precise : Attribute Error: LCAS.LabcarController.writeLogWindow() Here, LCAS.... is a COM...
5
by: johnny | last post by:
I am getting the following errors: File "H:\xampp\xampp\xampp\python\lib\httplib.py", line 679, in _send_output self.send(msg) File "H:\xampp\xampp\xampp\python\lib\httplib.py", line 646, in...
2
by: Jason | last post by:
Hi folks-- Basically, I have a pressing need for a combination of 5.2 "Sorting a List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects by an Attribute of the Objects" from the...
10
by: MeeMee | last post by:
Hi, I have a problem in one of the MS Access systems at work. I am using some database functions that requires the DAO reference. The system is working fine on my PC, however it occurs on 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.