Ali Ahmed
Ali Ahmed

Reputation: 183

When is a parameter necessary after self

i am new to python and I had a question about classes. After making a class, when you use def init(self), when is it necessary to have parameters after self. In some pygame I've seen that sometimes they included and sometimes not for example in the codes below for settings of game (Alien Invasion) and drawing a ship: I am confused that why is there a screen parameter, which is in the main program defining the display, and there is no parameter in settings. Please explain, thank you.

class Ship():
    def __init__(self, screen):

    # initializes the screen and set its starting postion
    self.screen = screen

    # now we load the image

    self.image = pygame.image.load("ship.bmp")
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

    # starting ship at each bottom of screen

    self.rect.centerx = self.screen_rect.centerx
    self.rect.bottom = self.screen_rect.bottom

def blitme(self):
    # drawing ship at its locations, use blitme
    self.screen.blit(self.image, self.rect)

class Settings():
    def __init__(self):
        self.screen_width = 1000
        self.screen_height = 700
        self.bg_color = (230, 230, 230)

Upvotes: 0

Views: 335

Answers (3)

Alxmrphi
Alxmrphi

Reputation: 229

I think you would benefit from just a simple example. Think about when you create an instance of a class. You don't have to provide any parameters if you just keep the class constructor as having the single 'self' argument.

my_dog = Dog()

However, just like with other functions, you might want to provide some information up front about the dog you are creating. Let's say whenever you want to create a Dog object, you want to provide its name and whether it barks and bites. When setting up your dog class, you can specify in the constructor that these should be specified when making an instance of the Dog class:

class Dog(object):
    def __init__(self, name, barks, bites):
        self.name = name
        self.barks = barks
        self.bites = bites

Now when you make an instance of the class, you specify these parameters upfront:

my_dog = Dog('Rover', barks=True, bites=False)

This means that you are in control of what information needs to be passed when creating an instance of your class. In this case, the author of the code wants a piece of information relating to the 'screen' to be used when creating an instance of the class. Other classes you write might not need any additional information, so they just use 'self'.

Just like when you write functions, you are in charge of what parameters they take and it's exactly the same case with classes.

Upvotes: 1

jpp
jpp

Reputation: 164773

A method is a function. Function parameters are optional. Whether or not a function should have a parameter depends on the purpose of the function. Each function should be designed for a specific task; some may require an argument, others may not.

Python object-oriented programming is designed so the first parameter of methods is the instance the method is called on. This enables a method to be called just like a regular function. Use of self as opposed to this_class_instance is just a convention, but one that you should probably follow for readability.

Upvotes: 0

SarahB
SarahB

Reputation: 155

parameter after "self" like self.image, allow you to init your Object "Ship" and use it outside the class. It defines attributes to characterize your object "Ship" It allow you to do Object-oriented programming (OOP) You can define all attributes you want like :

self.speed = 10 

if you want to init the move speed of your object.

you can check this doc : class python

But I think you have to read Object-oriented programming documentation to understand all the concept. In programmation it's very very important to understand that. Not only for Python but for all languages. (Java, C, C# ...).

Upvotes: 0

Related Questions