Python: modifying class from an inside function

  • Context: Python 
  • Thread starter Thread starter stargazer3
  • Start date Start date
  • Tags Tags
    Class Function Python
Click For Summary

Discussion Overview

The discussion revolves around modifying a class attribute from within a class method in Python. Participants explore how to correctly manipulate class and instance attributes, and clarify the differences between them. The scope includes technical explanations and corrections related to Python class behavior.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant initially presents a code snippet and expresses confusion about modifying a class attribute from a method.
  • Another participant suggests corrections, indicating that the method should use self.a instead of a, and that the instance should be created with t = Z().
  • It is noted that the method should be called with parentheses, t.mod(), to execute it properly.
  • A participant emphasizes the distinction between class attributes and instance attributes, suggesting a need for further understanding of these concepts.
  • A later post introduces a static method approach, demonstrating how to modify a class attribute directly and providing a working example with expected output.
  • Another participant acknowledges their earlier mistakes and confirms that their code is now functioning correctly.

Areas of Agreement / Disagreement

While some participants agree on the corrections needed for the initial code, there is no explicit consensus on the best approach to modifying class attributes, as different methods (instance methods vs. static methods) are discussed.

Contextual Notes

Participants mention the differences between class and instance attributes, indicating that understanding these distinctions is crucial for resolving the original question. There are also references to outdated syntax in Python, suggesting a need for awareness of language evolution.

Who May Find This Useful

This discussion may be useful for Python programmers, particularly those seeking to understand class and instance attribute manipulation, as well as those interested in the nuances of static methods.

stargazer3
Messages
44
Reaction score
3
I feel very stupid right now. Should be an easy question, but I googled it to no avail.
Basically, I have an class.object in my code which should be managed by a class.function
For example, I want the following code:

Code:
class Z:
        a=0
        def mod(self):
                self.a=self.a+1

t=Z
print Z.a
t.mod
print Z.a
t.a+=1
print Z.a

to return "0 1 2", instead of "0 0 1"
Can it be done in Python? If yes, what is the Python way of doing so? If no, why not?
 
Last edited:
Technology news on Phys.org
stargazer3 said:
I feel very stupid right now. Should be an easy question, but I googled it to no avail.
Basically, I have an class.object in my code which should be managed by a class.function
For example, I want the following code:

Code:
class Z:
        a=0
        def mod(self):
                a
                a=a+1

t=Z
print Z.a
t.mod
print Z.a
t.a+=1
print Z.a

to return "0 1 2", instead of "0 0 1"
Can it be done in Python? If yes, what is the Python way of doing so? If no, why not?

I think you have several things wrong.
First, inside the mod function, I think you should have: self.a = self.a + 1
Second, when you create the object t as an instance of class Z, you should have: t = Z()
This invokes the constructor of Z.
Third, when you call the function mod you should have: t.mod()
Fourth, don't you want print t.a instead of print Z.a ??
 
Oh, I'm so sorry, was writing it in a hurry!
Yes, it's working right now, thanks!
...
I think that my face is quite red now.
 
Also, you need to keep in mind the difference between a class and an instance of it...class Z is already an object in its own right that can be dealt even before creating the instance t...so, you actually have 2 things going on and you need to learn how to deal with them...you have class attributes and instance attributes...go back and re-read classes...
 
Code:
## @file static.py
import sys

class Z: # declares a "class object"
    a = 0 # inited when Z is seen first
    @staticmethod
    def mod():
        Z.a += 1

t = Z # t is an alias reference on the Z "class object"
sys.stderr.write('\t' + str(t) + '\n')

print(Z.a)
t.mod()
print(Z.a)
t.a += 1
print(Z.a)

u = Z() # u is a Z instance
sys.stderr.write('\t' + str(u) + '\n')

u.mod() # actually works, contrary to C++ or Java statics! 
sys.stderr.write('\t' + str(Z.a) + '\n')

Usage
Code:
python static.py

Output:
Code:
	__main__.Z
0
1
2
	<__main__.Z instance at 0x7f5233334488>
	3

Tested with python 2.7 and python 3.1 on x86_64 Debian "squeeze" Linux

Regards, S.

P.S.: Pls keep in mind that "print" without brackets is outdated.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 11 ·
Replies
11
Views
1K