![]() |
|
[Python] Extremely noob question - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Extremely noob question (/showthread.php?tid=6039896) |
Extremely noob question - SIM-2362662 - Jan 10, 2020 My code: Code class A:Error: Quote:Traceback (most recent call last): Why is this happening and how can I avoid this error? RE: Extremely noob question - Riԁdle - Jan 10, 2020 You're doing Code B.reveal()Code B().reveal()Code clB = B() RE: Extremely noob question - Nomad.Null - Jan 12, 2020 You forgot to provide your __init__() method for the class. Without an __init__() method, self is meaningless. The __init__() method is the "instantiation" method for each class, basically the instructions that are run each time a new instance of the class is created. The first argument of the __init__() method is always the name of the instance (conventionally "self" but it could be anything. You could make it "potato" if you like. Here is your fixed code. Code class A:And here is the potato code Code class A:RE: Extremely noob question - Riԁdle - Jan 12, 2020 (Jan 12, 2020 11:40 AM)True Demon Wrote: You forgot to provide your_init__() method for the class. Without an __init__() method, self is meaningless. This wasn't his problem. he defined Code classA=A()Code class ACode class Bso his classA variable is shared by all instances of class B RE: Extremely noob question - Nomad.Null - Jan 12, 2020 I suppose that could be the case, but it's a rather niche use of classes that I've rarely seen in practice. I think we're both making assumptions about his use-case here. Since he said upfront that it was a "noob" question, I assumed he wasn't terribly familiar with classes in the first place and was maybe just missing the more traditional solution. If that were the case, it would just be simpler to declare it as a static method like so: Code class A:No need for instantiation of the class or anything, that way. ¯\_(ツ)_/¯ I personally stay away from global class variables because they have a tendency to introduce really weird behavior that can make debugging a PITA. Any time you use global variables in classes, you risk making unintended changes to the class whenever you call the class itself or an instance of it. A static method is safer, and equally memory efficient in this case. 👍 RE: Extremely noob question - Riԁdle - Jan 12, 2020 (Jan 12, 2020 02:37 PM)True Demon Wrote: I suppose that could be the case, but it's a rather niche use of classes that I've rarely seen in practice. I think we're both making assumptions about his use-case here. Yeah, I just meant the error he got wasn't the result of not having an init method, i'm sure he'll learn something new from your previous post though, you explained it quite well. I doubt he made it a global variable on purpose aswell |