#🔒 classes

46 messages · Page 1 of 1 (latest)

karmic plover
#

how to i access a variable i made inside a method of a class on a different method of the same class?
i.e

def class(x, y, z)
...
...
def ice(self):
  variable1 = 0

def fire(self):
  return blahblah

if i wanted to access variable1 from ice in fire how would i do that?

tough oracleBOT
#

@karmic plover

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

low spire
#

it is not possible

#

variable1 exists only in the scope oif ice you can not access it from outside of this scope

#

you could make it an attribute of the instance (or the class)

#

or return it and call the method in the other method

lean torrent
#

You should define it as a class variable and reference it as self.variablename

#

Or use Sean's suggestions. Either will work

low spire
#

i mean

You should define it as a class variable and reference it as self.variablename
and
you could make it an attribute of the instance (or the class)
are the same 😂

lean torrent
#

Yes they are

#

But you also suggested returning the variable

karmic plover
#

whats it called when u add a uh

#

like static variable

#

@staticmethod?

#

i remember learning this but i forgot

#

a constant?

low spire
low spire
#
class Test:
    a = 4  # static variable
karmic plover
#

im basically doing some credit card thing where i need to make a way to track everytime a charge is made and keep a tracker so that after 10 calls the customer gets a $1 penalty on their balance

karmic plover
low spire
lofty lagoon
#

But first, you need to have a "class" statement which defines this as a class - not: def class. THen there is typically an init method to set up your variables, like this:

class MyClassName:
    def __init__(self, <any other parameters>):
        # any code to initialize instance variables
        # like:  self.variable1 = 0

Then each of your methods should have 'self' as a parameter.

Then you can refer to any instance variables in any other method using the self.variable name, like self.variable1

low spire
karmic plover
#

well i dont need this to be a variable

low spire
#

what do you not need to be a variable?

lean torrent
#

The question is how to access a variable. So I'm confused. It is a variable.

low spire
lofty lagoon
#

Assuming that you are going to create more than one credit card object, and you want to track the charges done on each card, then you would need something like this:

class CreditCard:
    def __init__(self):
        # any code to initialize instance variables
        self.charges = []  # define an empty list
        self.balance = 0 # define the total charges

Then you would probably have another method, maybe called new_charge. In that method, append the value of the new charge to the list (self.charges), and if the list now has over 10 items, then add one dollar to the balance (self.balance).

karmic plover
#

Um I think we’re using variable and attribute interchangeably

#

I don’t need it to be an attribute is what I meant

karmic plover
#

And added the counter to the charge class

#

So every time charges() is called

#

It does self.charges += 1

#

Then I added a method so that if self.charges > max_charges then self.balance += 1

#

I think that should work…

low spire
low spire
karmic plover
#

Because that’s a constant and it should never change

#

I guess I don’t even really need it?

#

I can just do self.charges > 10

tough oracleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.