Jase
Jase

Reputation: 517

python (wxPython) Need information about self and parent

This is probably a silly question, but all of the docs I have looked at at docs.python.org and at wx.python.org show that most init functions for an object take the form of: init(self, parent, etc, etc)

Ands most calls to these functions do not pass a value for self, or for parent.

So:

  1. if no values are passed, where do these values come from, and
  2. how could i refer to them from a function defined outside the class scope?

Example file 1 (PseudoClass.py):

class PseudoClass(someValidClass):
    def __init__(self, parent, title):
        someValidClass.__init__(self, parent, title)

and in another file:

import PseudoClass
instance = PsedoClass('My Title')
def someFunction(instance):
    (x,y) = <<something which returns the self and parent values>>

Is it SAFE to always use instance.parent, and how do i get the pointer for self?

EDIT: For some reason the code blockind didnt work the first time....grr..

EDIT: Okay, for all my forthought on how to ask this question, the research I've done in the time between my asking and now has shown me that the question was not right for what I wanted to know. On the other hand, nneonneo did answer (at least in part) what was asked here. To avoid wasting other peoples time in reviewing this I am marking it solved.

Upvotes: 0

Views: 991

Answers (1)

nneonneo
nneonneo

Reputation: 179412

Note that in Python, self is the object on the left hand of . (for a typical method call), i.e. foo.func(a,b) actually calls FooClass.func(foo, a, b). Thus, you can always refer to self outside the class by just using the instance itself.

parent is a different story...AFAIK, it should always be passed in as the first parameter to wxPython constructors (unless that behaviour is changed by derived classes). So, it seems improbable that PseudoClass('My Title') would even work.

Upvotes: 1

Related Questions