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 9145967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:38:39+00:00 2026-06-17T10:38:39+00:00

I have the following code: class VectorN(object): def __init__(self, param): if isinstance(param, int): self.dim

  • 0

I have the following code:

class VectorN(object):
    def __init__(self, param):
        if isinstance(param, int):
            self.dim = param
            self.data = [0.0] * param

        elif isinstance(param, tuple):
            self.dim = 3
            self.data = param
        #else:
            #raise TypeError("You must pass an int or sequence!")

    def __str__(self):
        return "<Vector" + str(self.dim) + ": " + str(self.data) + ">"

    def __len__(self):
        return len(self.data)


    def __setitem__(self, key, item): 
        self.data[key] = item

Now when i try to call the __setitem__ method using the following code,

w = VectorN((1.2, "3", 5))
w.setitem(0, 9.9)
print(z) 
print(w) 
print(z[0])
print(len(v))

it gives me:

AttributeError: ‘VectorN’ object has no attribute ‘setitem’

  • 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-17T10:38:41+00:00Added an answer on June 17, 2026 at 10:38 am

    That’s because __setitem__ is a magic method. It’s a special function that allows you to create container objects.

    Because it’s a magic method, you need not call it directly by its name- rather, built-in aspects of the Python language control its behavior. Note that it’s still just a normal method; you could call it by name, but then the syntax would be w.__setitem__(0, 9.9).

    By defining __setitem__ you can instead set a value like so: w[0] = 9.9.


    __setitem__, from A Guide to Python’s Magic Methods:

    Defines behavior for when an item is assigned to, using the notation self[nkey] = value.

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

Sidebar

Related Questions

Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following code: class outer { struct inner { int var1; int
I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code :- class A : public B { public: _container
I have the following code: class Test { public static void main(String[] args) {
I have the following code: class Employee { friend string FindAddr( list<Employee> lst,string name
I have the following code: class SomeClass {}; class SomeOtherClass { SomeClass& someObj; public:

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.