#🔒 classes
46 messages · Page 1 of 1 (latest)
@karmic plover
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.
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
You should define it as a class variable and reference it as self.variablename
Or use Sean's suggestions. Either will work
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 😂
i think adding as an attribute makes the most sense
whats it called when u add a uh
like static variable
@staticmethod?
i remember learning this but i forgot
a constant?
you did not said how you want to use the value so i wanted to tell you this possibility
define in in the class
class Test:
a = 4 # static variable
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
so it has to go right under the class?
no just not in a method like __init__ but you can define some methods first and then class variables
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
but this is the convention
well i dont need this to be a variable
what do you not need to be a variable?
The question is how to access a variable. So I'm confused. It is a variable.
for this you need a variable to count the calls
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).
Um I think we’re using variable and attribute interchangeably
I don’t need it to be an attribute is what I meant
Yeah I did this except I made charges 0
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…
what? about what are we talking?
this is an attribute
The max_charges
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
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.