0

Ive seen many tutorials on classes in python, but I'm trying to do something far more basic with them, but somehow it isnt working right.

I'm trying to make 3 dimensional points by putting them into the class's placeholder.

However when I try to print the output of make3Dpoint(x,y)

I get something like <__main__.point object at 0x02D8FA30>.

When I try to append this output to a list I just get nonetype.

Obviously I'm a beginner to this and I don't want to get into any advanced way of solving this (and I'm not allowed to for the class this is for). I don't want to modify the class itself.

Is there anyway to make this output usable?

class point():
    __slots__ = ('x','y','depth')

def calculate(x,y):
    z = x * y + 2 * x * y + 4
    return z

def make3Dpoint(x,y):
    z = calculate(x,y)
    point=point()
    point.x = x
    point.y = y
    point.depth = z
    return point
  • Your `make3Dpoint` should really be the `__init__` method of your `point` class (which by convention should be named `Point`). – chepner Jan 27 '13 at 19:07

2 Answers2

0

The list append method returns None, but the element is added to the list.

>>> a = [1, 2]
>>> print(a.append(3))
None
>>> a
[1, 2, 3]
iurisilvio
  • 4,868
  • 1
  • 30
  • 36
  • I meant to say that the list itself returned nonetype. I wasn't printing the .append results, but rather the list after the append. In your example, a would have returned nonetype. – user2016046 Jan 27 '13 at 19:30
0

Structure your code something like this:

def calculate(x,y):
    z = x * y + 2 * x * y + 4
    return z

class Point:
    __slots__ = ('x', 'y', 'depth')

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.depth = calculate(x,y)

points = []
points.append( Point(2,3) )
print points
chepner
  • 497,756
  • 71
  • 530
  • 681
  • ah, well I don't doubt that structure works, is it possible to do it without having an __init__ function inside the class? I'm trying not to modify the class at all. – user2016046 Jan 27 '13 at 19:32
  • It will work the way you currently have. But adding an `__init__` method to a class is hardly advanced; it's the standard way of initializing an object. – chepner Jan 27 '13 at 20:07
  • Alright I got it working. Thanks for the help. Ironically that ended up being the easiest part of the assignment. I think I messed up the actual calculate part lol. – user2016046 Jan 27 '13 at 23:01