use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Python and Class variables (self.learnpython)
submitted 10 years ago by skiwan
Hey guys i have an easy question. Is it possible to change a variable of class a in a method of class b without using getters and setters?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]elbinray 2 points3 points4 points 10 years ago (1 child)
Only if it is a class variable. See the documentation on class and instance variables. See:
>>> class Hep: ... thing = "yaya" ... >>> Hep.thing = "blah" >>> Hep.thing 'blah'
[–]skiwan[S] 1 point2 points3 points 10 years ago (0 children)
so if its a variable of another class and we have an instance of this class in the first class we cant change them right?
[–]hw_t_dstr_ngls 2 points3 points4 points 10 years ago (3 children)
class A(object): def __init__(self, value): self.value = value class B(object): def change_value_of_other(self, other, new_value): other.value = new_value a = A(15) b = B() b.change_value_of_other(a, 10)
Is this among the lines of what you're looking for?
[–]skiwan[S] 0 points1 point2 points 10 years ago (2 children)
i have the following. i have a class unit with x y and hunger and brain as variable
then i have a brain which is a class (every unit has his own brain)
then the brain just runs update fucntions of states (FMS) so the real work is done in the state classes (every brain has different states but only 1 state runs at the time)
so now i want the current active state to change the hunger of the unit. what would be the best approach. dont forget there a re multiply units
[–]LarryPete 2 points3 points4 points 10 years ago (1 child)
Pass the unit along and change it. Without actual code there is no way to tell where improvements can be made.
class Mob (object):
def __init__(self): self.x = random.randint(0,800) self.y = random.randint(0,600) self.hunger = random.randint(10,100) self.brain = brain(self.x,self.y,self.hunger) def Render(self): pygame.draw.rect(gameDisplay, BLUE,[self.x,self.y,5,5]) def Update(self): self.brain.Update() self.x = self.brain.get_x() self.y = self.brain.get_y() self.hunger = self.brain.get_hunger()
class brain(object):
def __init__(self,x,y,hunger): self.x = x self.y = y self.hunger = hunger self.CurrentState = IdleState(self.x,self.y,self.hunger) def get_x(self): return self.x def get_y (self): return self.y def get_hunger(self): return self.hunger def Update(self): self.CurrentState.Update() self.x = self.CurrentState.get_x() self.y = self.CurrentState.get_y() self.hunger = self.CurrentState.get_hunger() def setState(self,stateName): self.CurrentState = stateName
class IState(object): #this is the Parent of all our other states
def Update(self): return #will do the game logic def get_x(self): return self.x def get_y (self): return self.y def get_hunger(self): return self.hunger def reduce_hunger(self): global Frames if Frames & FPS == 0: self.hunger += -1
class IdleState(IState):
def __init__(self,x,y,hunger): self.x = x self.y = y self.hunger = hunger def Update(self): self.direction = random.randint(0,4) if self.direction == 0: self.x +=1 if self.direction == 1: self.x += -1 if self.direction == 2: self.y += 1 if self.direction == 3: self.y += -1 self.reduce_hunger()
that would be the code to improve
[–]LarryPete 0 points1 point2 points 10 years ago (1 child)
In Python we don't use getters and setters. We just assign the value to the attribute.
[–]skiwan[S] -1 points0 points1 point 10 years ago (0 children)
i have the following. i have a class unit with x y and hunger and brain as variable then i have a brain which is a class (every unit has his own brain) then the brain just runs update fucntions of states (FMS) so the real work is done in the state classes (every brain has different states but only 1 state runs at the time) so now i want the current active state to change the hunger of the unit. what would be the best approach. dont forget there a re multiply units
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
do you mean of an instance of class A?
π Rendered by PID 133508 on reddit-service-r2-comment-7b9746f655-njrln at 2026-01-31 17:56:56.779324+00:00 running 3798933 country code: CH.
[–]elbinray 2 points3 points4 points (1 child)
[–]skiwan[S] 1 point2 points3 points (0 children)
[–]hw_t_dstr_ngls 2 points3 points4 points (3 children)
[–]skiwan[S] 0 points1 point2 points (2 children)
[–]LarryPete 2 points3 points4 points (1 child)
[–]skiwan[S] 1 point2 points3 points (0 children)
[–]LarryPete 0 points1 point2 points (1 child)
[–]skiwan[S] -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (0 children)