Python: modifying class from an inside function

In summary, The conversation discusses how to properly manage and manipulate objects and attributes in Python. The correct use of classes and instances is emphasized, along with the use of static methods. The conversation also mentions the need to update outdated coding practices, such as using "print" without brackets.
  • #1
stargazer3
44
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
  • #2
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 ??
 
  • #3
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.
 
  • #4
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...
 
  • #5
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.
 

FAQ: Python: modifying class from an inside function

Can you modify a class from within a function in Python?

Yes, it is possible to modify a class from within a function in Python. This is because functions in Python have access to the global namespace, which includes the class definition.

How do you modify a class from an inside function in Python?

To modify a class from an inside function in Python, you can use the setattr() function, which allows you to set an attribute of an object. You can also use the __setattr__() method to directly modify the class.

What is the difference between using setattr() and __setattr__() to modify a class from an inside function in Python?

The setattr() function can be used to modify any object, while the __setattr__() method is specific to classes and can only be used to modify the attributes of a class.

Can you modify a class attribute from an inside function in Python?

Yes, you can modify a class attribute from an inside function in Python. You can use the vars() function to access the class attributes and then modify them using setattr() or __setattr__().

Is it considered good practice to modify a class from an inside function in Python?

It is generally not recommended to modify a class from an inside function in Python, as it can lead to unexpected behavior and make the code difficult to understand. It is better to modify the class outside the function or use inheritance to add new attributes or methods to a class.

Similar threads

Replies
10
Views
1K
Replies
2
Views
996
Replies
18
Views
1K
Replies
5
Views
2K
Replies
16
Views
1K
Replies
15
Views
2K
Replies
3
Views
693
Replies
11
Views
1K
Back
Top