classes in python with self parameter
self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
classes in python with self parameter
self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.
self in python
"""
Before potentially reading along I highly suggest you master how to
use parameters and arguments in python because if you haven't this can
get confusing. Otherwise read along:
The self argument is most frequently used in the __init__ function.
The __init__ function is a constructor function meaning that it
constructs objects in a class.
This is part of the object oriented programming (OOP) branch.
In this example ill be creating a person with values of name, gender and
age.
What 'self' essentailly is, is the class, so when referring to 'self'
you are refering to values in a class.
So 'self.age = age' is just adding a variable in the
class which has the same value as 'age'.
"""
# Creating the class
class Person:
# Creating the constructor method
def __init__(self, name, age, gender):
# defining variables
self.name = name
self.age = age
self.gender = male
# Creating Objects
Person1 = Person("Isabella", 27, "female")
Person2 = Person("Mikkel", 29, "male")
# Creating a list of people:
People = [ Person1, Person2 ]
"""
You can now use the list of people to well.. list people and their
details.
You could make a table of people by making the array 2 dimensional
which can eventually lead to a database,
but this isn't a post to get into that.
"""
"""
Thats essentially how to use constructors, you're creating an object
relative to topic per se. More examples / usages:
"""
class Person:
def __init__(self, name, age, gender):
# The 3 arguments can only be used within this function
# So you must attach it to the class using 'self'
self.name = name
self.age = age
self.gender = gender
def Function(self):
# To be able to use it all around the class.
print(self.name)
print(self.age)
P = Person("Jeff", 27, "male")
P.Function()
# Output:
# >>> Jeff
# >>> 27
#Or
print(P.name)
print(P.age)
# Output:
# >>> Jeff
# >>> 27
"""
So overall this comes to show that self.something is the exact same as
a varible but rather than a global variable or a variable within a
function scope, it's instead a class variable.
So when doing 'self.something' logically you can think of it as:
'class.something'
And thats it. To see a better way of coding the code above you can
carry on reading but although it's not necessary to know.
A better way of calling a class is to use the __call__ method.
This creates an inbuilt constant function within the class that can be
called, rather than creating a function as above.
"""
class Person:
def __init__(self, name, age, gender):
self.name=name
self.age=age
self.gender=gender
def __call__(self):
print(self.name)
print(self.age)
# Creating the object
P = Person("Jeff", 27, "male")
# Calling the call function:
P()
# Output:
# >>> Jeff
# >>> 27
# This makes everything much neater and compact.
# Have a wonderful day :)
why to use self in python
By using the “self” keyword we can access the attributes
and methods of the class in python.
It binds the attributes with the given arguments.
self is parameter in function and user can use another parameter
name in place of it.
But it is advisable to use self because,
it increase the readability of code.
self in python
class Class(parentClass):
def __init__(self,arg):
self.arg = arg
def function(self):
print(arg)
self python
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
python self nedir
'''
Created on 14 Jan 2017
@author: Ibrahim Ozturk
@author: www.ozturkibrahim.com
'''
class Musteri(object):
"""Bir banka musterisinin hesabindaki tutari kontrol etme. Musteri sinifi
asagidaki ozelliklere sahiptir :
Ozellikler:
isim : Musteri ismini tutan string turunde parametre
bakiye : Musterinin hesabinda halihazirdaki bakiyeyi gosteren float tipinde parametre.
"""
def __init__(self, isim, bakiye=0.0):
"""Girilen isim ile ve baslangic bakiyesi olarak sifiri koyan musteriyi olusturur."""
self.isim = isim
self.bakiye = bakiye
def paraCek(self, tutar):
"""Hesaptan ilgili tutarin cekilmesinin ardindan yeni bakiyeyi doner."""
if tutar > self.bakiye:
raise RuntimeError('Bakiye yetersiz.')
self.bakiye -= tutar
self.bilgiGoster()
return self.bakiye
def paraYatir(self, tutar):
"""Hesaba ilgili tutarin yatirilmasinin ardindan yeni bakiyeyi doner.."""
self.bakiye += tutar
self.bilgiGoster()
return self.bakiye
def bilgiGoster(self):
"""Ilgili musterinin ismini ve hesap bakiyesini ekrana basar.."""
print(self.isim + " isimli musteriye ait bakiye >> " + str(self.bakiye) + " TL'dir.")
musteri1 = Musteri('Ibrahim Ozturk', 500.0)
musteri1.paraCek(100.0)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us