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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:06:17+00:00 2026-06-03T21:06:17+00:00

class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: self.value.__add__(100) self.run() def run(self):

  • 0
class TestClass(object):
    def __init__(self):
        self.value = 100
        self.x = lambda: self.value.__add__(100)
        self.run()

    def run(self):        
        self.x()
        print self.value

t = TestClass()

#Output: 100

I would like to able to define a lambda function such as the one in TestClass and have it alter an instance variable. It would seem that the way the lambda is constructed means that it does not modify the original value. I suspect that this to do with Python’s reference strategy which I do more or less understand.

So accepting the flaws in what I have done, is there a similar way to get similar functionality? I ultimately need to define many methods like x and intend to keep them in a dictionary as they will form a simple instruction set. As far as I can tell I need either to use lambdas or exec to do what I want.

  • 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-03T21:06:18+00:00Added an answer on June 3, 2026 at 9:06 pm

    __add__ is not inplace, so the return value of TestClass.x is self.value + 100, but self.value is not altered. Try this:

    import random
    HATE_LAMBDAS = random.choice(True, False)
    class TestClass(object):
        def __init__(self):
            self.value = 100
            if HATE_LAMBDAS:
                def x():
                    self.value += 100
                self.x = x
            else:
                self.x = lambda: setattr(self, "value", self.value + 100)
            self.run()
    
        def run(self):        
            self.x()
            print self.value
    
    t = TestClass()
    
    #Output: 200
    

    Use the setattr to increment the value while still using a lambda. Beware however, lambda’s in python are one of its worst features. However, both methods work.

    Edit

    Just remebered something that you might find usefull! The standard library has a module called operator which implements standard operators as functions. If you plan on using lambdas a lot, you might like to investigate it.

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

Sidebar

Related Questions

Assume I have some simple class class TestClass: def doSomething(self): print 'Did something' I
My jpa looks like below public class TESTClass implements Serializable { ... private String
When I run the following test, I get an ArgumentOutOfRangeException: [TestClass] public class ReproduceException
What am I doing wrong here? import unittest class Test_1(unittest.TestCase): def SetUp(self): self.data =
import unittest from SFDC_Objects.Accounts import Accounts class case(unittest.TestCase): def setup(self): self.account=Accounts() self.account.account_init() def testcase(self):
When creating a class in Python, I can simply make a def __len__(self): method
@Entity public class TestClass implements Serializable{ private Integer id; private Set<String> mySet; @Id @GeneratedValue
Example: public class TestClass { public static void main(String[] args) { TestClass t =
I have two classes: testClass and castClass : class testClass { public: int field1;
Given I have a class with two constructors: public class TestClass { ObjectOne o1;

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.