4 Replies - 373 Views - Last Post: 17 October 2013 - 07:01 AM Rate Topic: -----

#1 dday9  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 86
  • Joined: 17-April 13

How to pass parameters in class

Posted 16 October 2013 - 03:19 PM

I come from a Visual Basic.net background and I've taught myself Python. Basically I'm trying to declare a class that will represent the reels on a slot machine. In vb.net I'd create the class like this:

Public Class Reel
   Public Property Reel As String
   Public Property Payout As Integer

   Private Sub New(ByVal msg As String)
      Me.Reel = msg
   End Sub

   Private Sub New(ByVal pay As Integer)
      Me.Payout = pay
   End Sub

   Private Sub New(ByVal msg As String, ByVal pay As Integer)
      Me.Reel = msg
      Me.Payout = pay
   End Sub

End Class


And initialize the class like this:
Dim reel1 As New Reel("Cherry | Cherry | Cherry", 5)


That would initialize a new instance of the class and set the two properties.

I've tried creating something like that in Python and came up with this:
class reel():
	reel = ""
	payout = 0
	
	def __init__(msg, pay):
		reel = msg
		payout = int(pay)


But apparently that isn't the way that you pass the parameters. Could somebody please help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: How to pass parameters in class

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: How to pass parameters in class

Posted 16 October 2013 - 03:36 PM

Python is a little strange in that all class functions are required to have at least 1 parameter, which is "self" or "Me". For example:
class reel():
     r = ""
     payout = 0
     def __init__(self, msg, pay):
             self.r = msg
             self.payout = int(pay)



As you can see the first parameter is the "self" or object reference for the current instance. To access member variables of a specific instance you need to use self.MEMBER_VARIABLE_HERE, otherwise it doesn't give you the current instance's member variable.

Hopefully that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: How to pass parameters in class

Posted 16 October 2013 - 04:20 PM

To amplify on this, a python function call like this:

foo.frobnicate(widget) 


passes to the function as a message

frobnicate(foo, widget)



And now foo is "self" in the body of the function call.

It's actually quite standard to pass the identity of the object to the function (this is how it finds its instance members). What's weird about python is that python does it explicitly.

View PostBetaWar, on 16 October 2013 - 05:36 PM, said:

Python is a little strange in that all class functions are required to have at least 1 parameter, which is "self" or "Me".


Just a word of caution, while you can call this parameter anything you want, the universal usage is "self", and anything else will be greatly unappreciated by anyone who comes across your code.
Was This Post Helpful? 1
  • +
  • -

#4 DblAAssassin  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 247
  • Joined: 11-May 13

Re: How to pass parameters in class

Posted 16 October 2013 - 05:29 PM

Think as self like a variable in an algebra problem.

Quote

6x + 2 = 14

As, you can see x is the placeholder is for 2.

Quote

6(2) + 2 = 14


Now, think about self, as the 'placeholder' if you will to the variable or instance, that can be called outside of the class, as you don't probably don't know the instance before you write the class, you use self as a placeholder. Does this help?

This post has been edited by DblAAssassin: 16 October 2013 - 05:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 dday9  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 86
  • Joined: 17-April 13

Re: How to pass parameters in class

Posted 17 October 2013 - 07:01 AM

Thanks for all of y'all's help in clearing it up for me! I completely get it now :]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1