![]() |
|
[Python] [SOLVED] Why is my function not defined? - 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] [SOLVED] Why is my function not defined? (/showthread.php?tid=5665590) |
[SOLVED] Why is my function not defined? - JuiceWater - Jul 9, 2017 A few hours ago I decided that I wanted to learn Python. I only have experience with static languages like C and Java, so I'm still trying to get used to Python's whitespace rules and dynamic variables, but for the most part I've been able to pick it up very quickly.
I've just learned the basics of how classes are defined, and I tried to make one myself: Code class dog:I always get this error: Code File "c:\Users\Juice\Workspace\python\learning\pythony.py", line 12, in <module>And in line 10, where I try to call print_info() from the constructor, it is underlined with the tooltip: Code E0602:Undefined variable 'print_info'Really, how is it not defined?❓ print_info() should have been defined before the interpreter even reached line 10, no? Any explanations would be much appreciated. 🤝 * - Vanagander - Jul 9, 2017 _init_ is a class constructor and the self parameter refers to an instance of the object._init_ is automatically called when memory is allocated to the object (when an object of the class is created). This might help to clear it up: Code class MyClass(object):The test() has to be bound to an object upon calling it. So to fix your code put self.print_info() instead of print_info() as such: Code class dog:* - JuiceWater - Jul 9, 2017 (Jul 9, 2017 01:28 PM)Vanagander Wrote: __init__ is a class constructor and the self parameter refers to an instance of the object._init_ is automatically called when memory is allocated to the object (when an object of the class is created). Now it makes perfect sense, self is used temporarily for each instance of an object, so if I didn't include the self parameter when calling the function I wouldn't have even been able to add more than 1 dog anyway. Thank you, and I'm sorry for wasting everyone's time. I feel like such an idiot. 🤦♂ * - arkoydas - Jul 9, 2017 Hey mate, I run your code in Pycharm debugger and I found out that the problem is in line: PHP Code Vanagander gave you a good solution. * - Malentor - Jul 10, 2017 I made this code , for practice just like you :) PHP Code good luck |