Reputation: 6785
I still have no idea what self and init do despite having exhausted hours upon hours of reading about. I finally gave up on trying to understand what it does and instead just know that I need to include it in my code (actually I'm not even sure if I need it).
In any case, in an effort to learn this stuff, I have taken an example program I found on some tutorial, and decided to try to replicate it on my own without referencing any examples.
I may be doing this completely wrong, but what I'm trying to do is have a function that is called when a button is pressed to create a new patient record. The part I'm stuck on is how to pass those variables (are they called attributes or variables?) from the button press function to the patients class? Do I just put that function as a method inside that class? Also, is there anyway I can create a new patient each time the button is pressed?
def buttonpress():
name = input("what's your name")
age = input("what's your age")
totalpatients +=1
class patients:
def __init__(self, name, age):
self.name = name
self.age = age
def displaypatient(self):
print self.age, self.name
firstpatient=patients(name, 16)
firstpatient.displaypatient()
Upvotes: 0
Views: 3727
Reputation:
You're trying to use basic Object Oriented Programming, but need to understand that classes define objects and have properties.
In the below code we define a Patient class:
class Patient:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
return "%s - %s"%(self.age, self.name)
This has a method called __init__
which is defined by Python as the method called when initialising a new object from a class, and has several parameters:
The second methods, is a method we can call on the object to have it return a way that we might want to print it out.
Below we have code that creates a patient, and prints a representation of it from the display method.
firstpatient=patients(name, 16)
print firstpatient.display()
Below is a section of code that could be used to build an array of patients for us to work with at a later stage.
patients = [] # make an array with nopatients.
def getAnotherPatient():
name = input("what's your name")
age = input("what's your age")
patients.append(Patient(name,age)) # Add a new patient
while somecondition:
getAnotherPatient()
Once we've build a list of patients in the patients
array, we could then loop through the objects within that and then manipulate or display them as per normal.
Upvotes: 1
Reputation: 180
I feel like I was in the same boat as you a few weeks ago, and although very shakey on them still, I'm going to try to explain self and init to you as best I can. Please be gentle, other commentors! ;)
I'm going to try to explain "self" first because it might make init more clear. When I want to make a class, I can do so by:
class Car:
# I can set attributes here ( maybe I want all my cars to have a:
size = 0
# well eventually my car object is going to need to do stuff, so I need methods -
# methods are like functions but they are only usable by the class that encompasses them!
# I create them the same way, just inside the indentation of the class, using def func():
def drive(self):
print "vroom"
# pretend this is the function that I would call to make the car drive.
Okay. We have some stuff to talk about with just this layout here. We've DEFINED what a car should do, but we havent made anything yet. (i promise this is all relevant!) In order to make an INSTANCE (a single occurrence) of car, we can assign the car object to a new variable:
myCar = car()
now I can use all the METHODS we defined in the car class - like drive! we would call that function by typing in:
myCar.drive()
this would print "vroom" I can make a second INSTANCE of the car() class in the same program (it would be a totally different object though) by doing:
newCar = car()
Now, here comes the inception part... I've made a really simple class, and classes get really big and scary and it's literally impossible to understand them all in one night, but I'm going to explain self to you now.
"myCar", the variable I used to hold the car object I made, becomes the "self" argument if I have other methods that need to refer back to that object. in essence, myCar.vroom() is the same as saying self.vroom(), if I needed to refer to .vroom() in ANOTHER method for the class.
wrapping it up we have something that looks like this:
class Car:
size = 0 # a global attribute for all car objects
def drive(self): #self is the argument!
print "vroom!"
myCar = car() # we've initialized the class but havent used it here yet!
myCar.drive() # this prints "vroom"
another way to think of this would be saying the argument, just like in a normal function, is self is just a placeholder for whichever object is calling the function.
Now, if that made sense awesome, if not I'll edit it again. def init(self): is using the same theory, of taking a single object you made from the class, and just feeding it instructions to do every time the class creates an object.
class car:
def __init__(self): # needs self, to refer to itself, right?
self.name = name # now we can assign unique variables to each INSTANCE of the car class.
you can take def init and do some crazy stuff with it, like inside it you can immediately call other methods and stuff. basically, its like saying 'Hey object! You are alive, go check what is inside the init function, you need to have all that stuff! GO!
Let me know if this helps, or if I can make anything more clear. like I said, I barely just wrapped my head around all this, so maybe my explanation needs some work. Cheers :)
Upvotes: 2
Reputation: 179422
A class consists basically of data (attributes) and a collection of functions (methods) to operate on that data. Each instance of a class has its own copy of the data, but shares the methods.
Thus, every* method of a class operates on some instance of the class, so the method needs to know what instance it is operating on. That's what self
is for -- self
just means the instance that a method is working with.
So, for example:
class House:
def renovate(self):
''' Renovate a house, increasing its value. '''
self.value += 10000
oldhouse = House() # make a House instance that has no attributes
oldhouse.value = 100000 # set an attribute of the House
mansion = House()
mansion.value = 10000000
oldhouse.renovate() # self will be set to oldhouse, so that oldhouse's value goes up
print oldhouse.value # prints 110000
print mansion.value # prints 10000000: it hasn't changed because renovate knew what it was operating on
Now, in this example, note that I set .value
outside the class. You can do that (Python lets you set any attributes on almost any instance), but it's kind of cumbersome to do that for every class.
This is where __init__
comes in. It's a method with special meaning (hence the double underscores). In this case, it's a method which is called when the class gets created (when you call the class with parens to make a new instance). init
here stands for initialization
: it's a function that initializes the class attributes. Any arguments it takes (beyond self
) will be expected when you create the class.
So, we could change our example to this:
class House:
def __init__(self, value):
self.value = value # set attribute
def renovate(self):
''' Renovate a house, increasing its value. '''
self.value += 10000
oldhouse = House(100000) # make a House instance with the value set
mansion = House(10000000) # make another House instance with the value set
oldhouse.renovate()
print oldhouse.value # prints 110000
print mansion.value # prints 10000000
and it would work the same, but be somewhat nicer (since we don't have to keep setting .value
on new House
s).
* except staticmethods and classmethods, and special cases like __new__
Upvotes: 0