Hrvoje T
Hrvoje T

Reputation: 3913

How to tell what is a method and what is an object constructor in this class in Python?

I'm a beginner in Python and PySide. Can someone explain me how to recognize what is an object constructor and what is a method in this class (e.g. QLCDNumber(self) vs addWidget(argv)) and therefore why not calling self.vbox instead of vbox?

import sys 
from PySide import QtGui, QtCore

class App(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.init_ui()

    def init_ui(self):
        lcd = QtGui.QLCDNumber(self)
        sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)

        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(lcd)
        vbox.addWidget(sld)

Upvotes: 1

Views: 142

Answers (3)

badp
badp

Reputation: 11813

The difference between "methods" and "object constructors" in Python is very thin. You must be getting thoroughly confused with Java here.

Consider this:

class A():
  def __init__(self, bar):
    baz = bar + bar
    self.egg = baz + baz

def B(tuna):
  return A(tuna)

What happens is baz is a mere variable and only egg hangs around after A.__init__() ends.

>>> A("foo").egg
"foofoofoofoo"
>>> A("foo").baz # exception

Also, the return values of A() and B() are indistinguishable.

In Python, way moreso than Java, everything is an object and with a few corner cases there is no distinction between functions and methods. If you really really really did need to check, this is the way to go:

>>> import types
>>> type(A) == types.ClassType and type(B) != types.ClassType
True
>>> type(A) != types.FunctionType and type(B) == types.FunctionType
True
>>> type(A.__init__) == types.MethodType and type(B) != types.MethodType
True

...but the need to do such introspection is quite rare.

Upvotes: 1

Brambor
Brambor

Reputation: 664

Constructor __init__() is called when you initialise the class

class example:
    def __init__(self):
        print("example")
    def init(self):
        print("example No. 2")

So:

>>>a = example
... "example"
>>>a.init()
... "example No. 2"

The self is something that class have stored inside itself, not globaly but it will not 'disaper'

class Human:
    def __init__(self, gender, name):
        self.age = 0
        self.gender = gender
        self.name = name
    def aging(self):
        self.age += 1

humans = []
humans.append(Human("male", "Jack")) # his age is 0 and gender is male
humans.append(Human("female", "Jesica"))
for human in humans:
    human.aging() # age is increasing

Upvotes: 1

TWReever
TWReever

Reputation: 357

Class constructors are defined by the method __init__. All non-static class methods (including the constructor) take a reference to the object itself in the form the self parameter, which is the first parameter passed.

You'd create a new instance of the App class by doing the following:

app = App()

Then to call the method init_ui you'd do the following:

app.init_ui()

If lcd, sld and vbox are all member variables of the App class then you'll need to access them as self.vbox within the methods.

Upvotes: 1

Related Questions