Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8269487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:14:30+00:00 2026-06-08T06:14:30+00:00

I am new in Python and in OOP in general. I have an error

  • 0

I am new in Python and in OOP in general. I have an error "...instance has no attribute '__getitem__'", and I understand that the object I have created is not a list. How can I make to be a list object. Here is the class file:

#!/usr/bin/python -tt

import math, sys, matrix, os

class Point:
    'Class for points'
    pointCount = 0

    def __init__(self, x, y, z):
        'initialise the Point from three coordinates'
        self.x = x
        self.y = y
        self.z = z
        Point.pointCount += 1

    def __str__(self):
        'print the Point'
        return 'Point (%f, %f, %f)' %(self.x, self.y, self.z)

    def copyPoint(self, distance):
        'create another Point at distance from the self Point'
        return Point(self.x + distance[0], self.y + distance[1], self.z + distance[2])

    def __del__(self):
        'delete the Point'
        Point.pointCount -= 1
        #print Point.pointCount
        return '%s deleted' %self

I need to have it as a point with three coordinates inside (x, y, z), and those coordinates must be “callable” like in a list instance with [].

I have read similar topics but did not understand much. Please describe it in simple words and with examples.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:14:32+00:00Added an answer on June 8, 2026 at 6:14 am

    I suggest you consider making your Point class using the collections.namedtuple factory function which will make it a subclass of the the built-in tuple class. This will save you some boiler-plate work. namedtuple class have attributes that can be accessed both by name, such as p.x and indexed, like p[0].

    They are also very memory efficient like tuples, which may be important if you’re going to have a lot of class instances.

    You can further specialize what is returned by subclassing it, or use the verbose option to capture the source code and modify that as necessary.

    There’s an example in the documentation linked to above showing it being used to create a 2D Point class, which seems like it could be very helpful in your specific use-case.

    Here’s an example showing how one could define a custom 3D Point class via subclassing:

    from collections import namedtuple
    
    class Point(namedtuple('Point', 'x y z')):
        __slots__ = ()  # prevent creation of instance dictionaries to save memory
        point_count = 0  # instance counter
    
        def __init__(self, *args):
            super(Point, self).__init__(*args)
            Point.point_count += 1
    
        def distance(self, other):
            return sum((self[i]-other[i])**2 for i in xrange(len(self))) ** 0.5
    
        def copy_point(self, distance):
            'create another Point at distance from the self Point'
            return Point(*[dimension+distance for dimension in self])
    
    p1 = Point(0, 0, 0)
    print 'p1:', p1
    p2 = p1.copy_point(20)
    print 'p2: Point(%s)' % ', '.join(str(p2[i]) for i in xrange(len(p2)))
    print 'distance p1 <-> p2: %.3f' % p1.distance(p2)
    

    Output:

    p1: Point(x=1, y=2, z=3)
    p2: Point(21, 22, 23)
    distance p1 <-> p2: 34.641
    

    Note that by using namedtuple you don’t have to implement a __getitem__() yourself, nor write a __str__() method. The only reason an __init__() was needed was because of the need to increment the class instance counter which was added — something that namedtuples don’t have or do by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with using class instance in Python. Ive created a new class
Very new to python and can't understand why this isn't working. I have a
I am new python programmer,what I have understood so far,yield keyword returns an object
I am not that really new in Programming but in Python I am really
New to Python, have a simple, situational question: Trying to use BeautifulSoup to parse
Brand new to Python (and programming in general), if this is simple and/or answered
I am new to Python and have been studying its fundementals for 3 months
I'm a new to Python. I have installed Python 2.7 64 bit on Win7
I'm new to Python and it's OOP stuff and can't get it to work.
well I'm new to SO, OOP and python too, so please be gentle ;)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.