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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:31:02+00:00 2026-05-20T04:31:02+00:00

If you have the following class: class Foo(object): def __init__(name): self.name = name And

  • 0

If you have the following class:

class Foo(object):

    def __init__(name):
        self.name = name

And you use it like this in a file called check_foo.py

with Foo("naming it"):
    print Foo.name


with Foo("naming another"):
    print Foo.name

If you import check_foo and run dir(check_foo) you will only get a single check_foo.Foo module.

I know that PEP 343 mentions that you can do something like:

with Foo("naming it") as naming_it:
    print naming_it.name

And that it would get instantiated properly in check_foo as check_foo.naming_it but my question is it is possible to work around this and set the name dynamically.

I’m playing around with a proof of concept and want to know how far I can get with the above idea.

Could it be possible to name the instance using the string I am passing to Foo ?

Note: I am also aware about withhacks. Let’s not suggest I take a look at that 🙂

  • 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-20T04:31:02+00:00Added an answer on May 20, 2026 at 4:31 am

    I’m not sure if this is the sort of hackery that you are looking for…

    import inspect
    
    class renameable(object):
      def rename_me(self, new_name):
        for stack_frame in inspect.stack()[1:]:
          frame_object = stack_frame[0] # frame is the first object in the tuple
          for (name, value) in frame_object.f_locals.iteritems():
            if value is self:
              old_name = name
              matched_frame = frame_object
              break
          if matched_frame:
            break
        if matched_frame:
          matched_frame.f_locals[new_name] = matched_frame.f_locals[old_name]
          del matched_frame.f_locals[old_name]
    

    I doubt that this is a complete solution, but it does allow you to change one binding of a value to a name. It changes the name that is bound to the value which is closest to the call of rename_me. For example:

    >>> import blah
    >>> x = blah.renameable()
    >>> x
    <blah.renameable object at 0x1004cb790>
    >>> x.rename_me('y')
    >>> x
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'x' is not defined
    >>> y
    <blah.renameable object at 0x1004cb790>
    >>>
    

    I’m not sure if this is better or worse than using withhacks but it does delve into a seldom explored module in the library.

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

Sidebar

Related Questions

I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name
I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
I have the following example code: class A(object): def __init__(self, id): self.myid = id
If I have the following code: class Foo(object): bar = 1 def bah(self): print(bar)
I have the following python code: class FooMeta(type): def __setattr__(self, name, value): print name,
I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
I have the following c# classes: class A : Object { foo() {} }
Basically, I have the following so far: class Foo { public override bool Equals(object

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.