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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:10:18+00:00 2026-06-06T04:10:18+00:00

i have this code: def some_method(): #i need to get some attrs :( return

  • 0

i have this code:

def some_method():
    #i need to get some attrs :(
    return 'blabla'

setattr(parent, 'new_attr', some_method)

it’s there a way to get other attributes of the parent inside the some_method function?

Thanks in advance!

Update: (solution)

I solved in this way, i don’t know if the best.

def some_method(parent):
    # print or do something parent.xy_attribute
    return 'blabla'

parent.new_attr = some_method(parent)

and it works!

Thanks again!

  • 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-06T04:10:20+00:00Added an answer on June 6, 2026 at 4:10 am

    If parent were a class, then you can access its attributes like you would with any other method of a class, by giving the self parameter and using that.

    If it isn’t a class, perhaps the best way to handle this would be to write your function within a wrapper/factory/something instead of as a method directly.

    def some_method_wrapper(parent):
    
        def some_method():
            #access any parent attribute 
            #by using the parent positional argument passed to the function
            print parent.desired_attribute
            return 'blabla'
    
        setattr(parent, 'new_attr', some_method)
    

    You can then use parent.new_attr() and it’ll be able to print/manipulate/whatever any parent attributes you want.

    Here’s a more fleshed out example of how you might use this to manipulate a counter on a function (or, say, flush a function cache).

    def parent_function(numbers):
        # do something with numbers
        parent_function.counter += 1
        return sum(numbers)
    
    parent_function.counter = 0
    
    def add_reset_function(parent):
    
        def reset_counter():
            parent.counter = 0
    
        setattr(parent, 'reset', reset_counter)
    
    # call parent_function a few times
    
    numbers = [1, 2, 3]
    for i in range(4): parent_function(numbers)
    print parent_function.counter # 4
    
    add_reset_function(parent_function)
    
    # call the reset function just added
    parent_function.reset()
    
    print parent_function.counter # 0
    parent_function(numbers)
    print parent_function.counter # 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: def paintEvent(self, paintEvent): self._painter.begin(self) try: while True: color, rectangle =
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
i have this code (on the fly compression and stream) @cherrypy.expose def backup(self): path
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
Let's say I have this code snippet running. class Song def initialize(name, artist, duration)
I have this simple code in my user_controller.rb file #listing all users def index
I have this in my application_helper.rb : def bbcode(text) # Code snippets text.gsub!(/\[code=?[']?(.*?)[']?\](.*?)\[\/code\]/mis) {
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr

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.