Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60650 > unrolled thread

error

Started byspeen saba <moonlightmadness86@gmail.com>
First post2013-11-27 16:37 -0800
Last post2013-11-27 21:30 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  error speen saba <moonlightmadness86@gmail.com> - 2013-11-27 16:37 -0800
    Re: error Cameron Simpson <cs@zip.com.au> - 2013-11-28 11:55 +1100
    Re: error Dave Angel <davea@davea.name> - 2013-11-27 21:30 -0500

#60650 — error

Fromspeen saba <moonlightmadness86@gmail.com>
Date2013-11-27 16:37 -0800
Subjecterror
Message-ID<f72cc61d-eb91-459a-8099-3e2b1be743ec@googlegroups.com>
Hello there,
   I am attending the lectures, and written the codes according to lectures. Professor's codes are working fine, but my one gives an error. I am new to python. Please it's a humble request, just be easy on me while making me understand about the code. It will be much appreciated.



import math
##points as lists
def addPoints(p1, p2):
	r = []
	r.append(p1[0]+p2[0])
	r.append(p1[1]+p2[1])
	return r

p = [1,2]
q = [3,1]
r = addPoints(p,q)
print r

 ##points as classes

class cartesianPoint():
  pass

cp1 = cartesianPoint()
cp2 = cartesianPoint()
cp1.x = 1.0
cp1.y = 2.0
cp2.x = 3.0
cp2.y = 1.0

def samePoint(p1,p2):
  return (p1.x == p2.x) and (p1.y == p2.y)

def printPoint(p):
  print '(' + str(p.x) + ', ' +str(p.y) + ')'

class polarPoint:
  pass
pp1 = polarPoint()
pp2 = polarPoint()
pp1.radius = 1.0
pp1.angle = 0
pp2.radius = 2.0
pp2.angle = math.pi / 4.0


class cPoint():
  def __init__(self,x,y):
    self.x = x
    self.y = y
    self.radius = math.sqrt(self.x*self.x + self.y*self.y)
    self.angle = math.atan2(self.y, self.x)
  def cartesian(self):
    return (self.x, self.y)
  def polar(self):
    return (self.radius, self.angle)
  def __str__(self):
    return '(' + str(self.x) + ', ' + str(self.y) + ')'
  def __cmp__(self,other):
    return (self.x == other.x) and (self.y == other.y)


And below is the error. Evrything works fine untill class polar point, but when I try to pick point (instance) p in the list i.e x,y (1,2,3,1). It does not work. I mean p.x gets the error where it should give me the value of x. And i know this error will go all the way down to def__cmp if i dont fic it from top.


   >>> p.x

Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    p.x
AttributeError: 'list' object has no attribute 'x'
>>> p.y

Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    p.y
AttributeError: 'list' object has no attribute 'y'

Help will be much appreciated, thanks in advance.

[toc] | [next] | [standalone]


#60651

FromCameron Simpson <cs@zip.com.au>
Date2013-11-28 11:55 +1100
Message-ID<mailman.3331.1385600125.18130.python-list@python.org>
In reply to#60650
On 27Nov2013 16:37, speen saba <moonlightmadness86@gmail.com> wrote:
> Hello there,
>    I am attending the lectures, and written the codes according to lectures. Professor's codes are working fine, but my one gives an error. I am new to python. Please it's a humble request, just be easy on me while making me understand about the code. It will be much appreciated.
> 

Well, right at the top of your code you set "P" to be a list:

> p = [1,2]

Are you sure you don't want to print pp1.x or pp2.x or cp1.x or cp2.x?

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

Achilles, a PHD student, had to choose between having a passionate but poor
life as an academic or a dull but rich life as a IT professional yuppie.
Legend has it that he chose poverty.

[toc] | [prev] | [next] | [standalone]


#60660

FromDave Angel <davea@davea.name>
Date2013-11-27 21:30 -0500
Message-ID<mailman.3335.1385605784.18130.python-list@python.org>
In reply to#60650
On Wed, 27 Nov 2013 16:37:37 -0800 (PST), speen saba 
<moonlightmadness86@gmail.com> wrote:
> p = [1,2]

> And below is the error. Evrything works fine untill class polar 
point, but when I try to pick point (instance) p in the list i.e x,y 
(1,2,3,1). It does not work. I mean p.x gets the error where it 
should give me the value of x. And i know this error will go all the 
way down to def__cmp if i dont fic it from top.

>    >>> p.x

> Traceback (most recent call last):
>   File "<pyshell#46>", line 1, in <module>
>     p.x
> AttributeError: 'list' object has no attribute 'x'

You have 4 different implementations, but you're mixing the first 
with the others.  Since p is a list, you need to use list semantics,  
p [0] and p [1]. The .x attribute is not defined on a list.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web