Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Extremely noob question

My code:

Code
class A:
    def A_display(self, msg):
        print(msg)

class B:
    classA=A()
    def reveal(self):
        self.classA.A_display("Calling Inner class function from Outer class")

clB=B()
B.reveal()

Error:
Quote:Traceback (most recent call last):
File "C:/Users/ghioc/Desktop/PythonWorld/CAM4/MAIN.py", line 44, in <module>
B.reveal()
TypeError: reveal() missing 1 required positional argument: 'self'


Why is this happening and how can I avoid this error?
You're doing
Code
B.reveal()
when you should be doing
Code
B().reveal()
or
Code
clB = B()
clB.reveal()
i like python lua js
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:
    def __init__(self):
        pass

    def A_display(self, msg):
        print(msg)

class B:
    def __init__(self):
        self.classA = A()

    def reveal(self):
        self.classA.A_display("Calling Inner class function from Outer class")

b = B()
b.reveal()

And here is the potato code

Code
class A:
    def __init__(potato):
        pass

    def A_display(potato, msg):
        print(msg)

class B:
    def __init__(potato):
        potato.classA = A()

    def reveal(potato):
        potato.classA.A_display("Calling Inner class function from Outer class")

b = B()
b.reveal()
Malware Dev & Grey Hat available for contract work. DM if you need me.

OSCP, OSCE, OSEE, OSWE, OSWP, CEH, CISSP

Yeah, I've been around a while
(Jan 12, 2020 11:40 AM)Nomad.Null Wrote:  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.

-snip-

This wasn't his problem.
he defined
Code
classA=A()
outside of the __init_
method because he wanted it to be a shared global variable for the classes (probably for memory purposes), he didn't want to initialize a new
Code
class A
object every time he initializes a new
Code
class B
object. assuming he did it on purpose lol

so his classA variable is shared by all instances of class B
i like python lua js
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:
   @staticimethod
   def A_display(msg):
       print(msg)

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. 👍
Malware Dev & Grey Hat available for contract work. DM if you need me.

OSCP, OSCE, OSEE, OSWE, OSWP, CEH, CISSP

Yeah, I've been around a while
(Jan 12, 2020 02:37 PM)Nomad.Null 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.

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:
   @staticimethod
   def A_display(msg):
       print(msg)

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. 👍

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
i like python lua js


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by ๖ۣۜOpulenceJul 23, 2014 12:54 PM 2 402 Jul 23, 2014 12:54 PM
Last Post: ๖ۣۜOpulence
[Python] by #SkylineMay 6, 2013 08:11 AM 5 374 May 6, 2013 08:11 AM
Last Post: jeetjain2345
[Java] by ZonatedJan 18, 2013 02:22 PM 6 131 Jan 18, 2013 02:22 PM
Last Post: Zonated