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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:40:47+00:00 2026-05-17T18:40:47+00:00

I have programmed in Python for a while, and just recently started using Ruby

  • 0

I have programmed in Python for a while, and just recently started using Ruby at work. The languages are very similar. However, I just came across a Ruby feature that I don’t know how to replicate in Python. It’s Ruby’s freeze method.

irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a[1] = 'chicken'
=> "chicken"
irb(main):003:0> a.freeze
=> [1, "chicken", 3]
irb(main):004:0> a[1] = 'tuna'
TypeError: can't modify frozen array
        from (irb):4:in `[]='
        from (irb):4

Is there a way to imitate this in Python?

EDIT: I realized that I made it seem like this was only for lists; in Ruby, freeze is a method on Object so you can make any object immutable. I apologize for the confusion.

  • 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-17T18:40:47+00:00Added an answer on May 17, 2026 at 6:40 pm

    You could always subclass list and add the “frozen” flag which would block __setitem__ doing anything:

    class freezablelist(list):
        def __init__(self,*args,**kwargs):
            list.__init__(self, *args)
            self.frozen = kwargs.get('frozen', False)
    
        def __setitem__(self, i, y):
            if self.frozen:
                raise TypeError("can't modify frozen list")
            return list.__setitem__(self, i, y)
    
        def __setslice__(self, i, j, y):
            if self.frozen:
                raise TypeError("can't modify frozen list")
            return list.__setslice__(self, i, j, y)
    
        def freeze(self):
            self.frozen = True
    
        def thaw(self):
            self.frozen = False
    

    Then playing with it:

    >>> from freeze import freezablelist as fl
    >>> a = fl([1,2,3])
    >>> a[1] = 'chicken'
    >>> a.freeze()
    >>> a[1] = 'tuna'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "freeze.py", line 10, in __setitem__
        raise TypeError("can't modify frozen list")
    TypeError: can't modify frozen list
    >>> a[1:1] = 'tuna'
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "freeze.py", line 16, in __setslice__
        raise TypeError("can't modify frozen list")
    TypeError: can't modify frozen list
    >>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a python newbie but have programmed a while in other languages. I have
I have programmed in HTML, Java, CSS, C++, VB, an Python. I'm looking to
I have never programmed in visual basic before and my boss just told me
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.
I am new to python and graphics but have programmed before. According to http://en.wikipedia.org/wiki/Transformation_matrix#Rotation
Disclaimer: I am new to python and django but have programmed in Drupal I
It's been a while since I programmed in C++, and after coming from python,
I have mostly programmed in Python, but I am now learning the statistical programming
I know a bunch of scripting languages, (python, ruby, lua, php) but I don't
I've recently started learning Python (long time Java programmer here) and currently in the

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.