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

  • Home
  • SEARCH
  • 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 3393882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:06:14+00:00 2026-05-18T04:06:14+00:00

Before I start, I’m already aware that object immutability in Python is often a

  • 0

Before I start, I’m already aware that object immutability in Python is often a bad idea, however, I believe that in my case it would be appropriate.

Let’s say I’m working with a coordinate system in my code, such that each coordinate uses a struct of X, Y, Z. I’ve already overloaded subtraction, addition, etc. methods to do what I want. My current problem is the assignment operator, which I’ve read cannot be overloaded. Problem is when I have the following, I do not want A to point to the same point as B, I want the two to be independent, in case I need to overwrite a coordinate of one but not the other later:

B = Point(1,2,3)
A = B

I’m aware that I can use deepcopy, but that seems like a hack, especially since I could have a list of points that I might need to take a slice of (in which case it would again have a slice of point references, not points). I’ve also considered using tuples, but my points have member methods I need, and a very large portion of my code already uses the structs.

My idea was to modify Point to be immutable, since it’s really only 3 floats of data, and from doing some research _new _() seems like the right function to overwrite for this. I’m not sure how to achieve this though, would it be something like this or am I way off?

def __new__(self):
    return Point(self.x, self.y, self.z)

EDIT:
My bad, I realized after reading katrielalex’s post that I can’t modify a parameter of immutable object once it has been defined, in which case it’s not a problem that both A and B point to the same data since a reassignment would require creation of a new point. I’d say that katrielalex’s and vonPetrushev’s posts achieve what I want, I think I’ll go with vonPetrushev’s solution since I don’t need to rewrite all my current code to use tuples (the extra set of parentheses and not being able to reference coordinates as point.x)

  • 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-05-18T04:06:15+00:00Added an answer on May 18, 2026 at 4:06 am

    In conjunction with katrielalex’s suggestion, making the Point a named tuple would be good as well. Here I’ve just replaced the tuple parent with namedtuple('Point', 'x y z') – and that’s enough for it to work.

    >>> from collections import namedtuple
    >>> class Point(namedtuple('Point', 'x y z')):
    ...     def __add__(self, other):
    ...             return Point((i + j for i, j in zip(self, other)))
    ...
    ...     def __mul__(self, other):
    ...             return sum(i * j for i, j in zip(self, other))
    ...
    ...     def __sub__(self, other):
    ...             return Point((i - j for i, j in zip(self, other)))
    ...
    ...     @property
    ...     def mod(self):
    ...             from math import sqrt
    ...             return sqrt(sum(i*i for i in self))
    ...
    

    Then you can have:

    >>> Point(1, 2, 3)
    Point(x=1, y=2, z=3)
    >>> Point(x=1, y=2, z=3).mod
    3.7416573867739413
    >>> Point(x=1, y=2, z=3) * Point(0, 0, 1)
    3
    >>> Point._make((1, 2, 3))
    Point(x=1, y=2, z=3)
    

    (Thanks to katrielalex for suggesting to extend the namedtuple rather than copying the code produced.)

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

Sidebar

Related Questions

before I start I want to point out that I tagged this question as
Before I start, I want to make it clear that my code is working
Before I start I'd just like to state that the code created by ASP.NET
Before you start reading... You should know that there are many questions below... I
Before start let me tell my experience: I am experienced with C#.NET, web services,
I want to get one record before start date and end date DtpFrom means
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
I usually try to do TDD with not much analysis (no diagrams) before start
I am working on a Media up-loader which uploads images to Server. Before start
(Before I start, yes I have asked a similar question before; unfortunately due to

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.