Code Classes Notes
Code Classes Notes
-
data functions
logically group data functions class are attributes methods
-
of a +
init method -
class
Employee :
definit - (self ,
first last
, ,
pay) :
. first
self =
first
&company . com
self
pay pay
=
,
self email
.
=
first +!+ last + company com' .
print (emp-1 .
email)
↑
accessed through RAISES # OF EMPLOYEES
class or an-
the
instance
class
of the -
defined
using
self (in a method) -
defined in init -
func
craise amount
-
-
self. raise -
amount
amount
or
pre defined
-
at the top of
raise
0)
-
class
Employee Employee : the
page (num-of-emps
,
=
when
using
def
self raise
.
instead of
- amount
apply-raise : . first
self =
first
Employee
it
.
allows you
raise amountself
to change
.
pay
=
int
(self-pay * self raise.
self email
.
=
first +!+ last + company com' .
Employee num-of-emps +=
.
1
METHODDS -
-
class methods
used when method about the class itself
a isn't
really an instance of a class but about
-
-
the class is
automatically set as the first
argument (by convention , class is represented by "cls")
Way #1 (to use the class method) modifying the class variable "raise-amount was modified
using
-
a class method
to access a class variable "cs" before
you have to write specifying the actual name
-
example (from vid) first and last names and separated by hyphen (t
: a
pay are
-
a class method can be created to return the class w/ the specific values passed in that are
STATIC
METHODS
different from
regular methods+ class something automatically passed
methods in that it doesn't have
-
in as the first
argument (class or instance not
IS
passed in)
-created by "Abstatic method creating the static method
adding the line before
-
have
logical connections to the class but don't need class (cs) or instance (self) as an
argument
-
when
you are sure that you don't make use of the class or instance within the method it's
,
For full tutorial and more details visit Python OOD Tutorial " by Corey Shaefer
specialized parts of a class but still needs the general info of the parent class
-
DEMO : dev-1 =
Developer (An ,
'Trans 500, 000 .
, Python')
print (dev-1 full name () .
*
↑ Developer--init-- func.
takes first
Developer doesn't have the
last
, ,
pay prog-log
,
-
method fullname but this code , still ↑
inherited from
runs
successfully and
prints "An Trans
parent func. programming
because this Developer subclass inherits language
class
from its parent ,
Employee
-
METHOD RESOLUTION ORDER : now python looks for methods when subclasses are used
python "walks up the chain of inheritance" to look for methods that aren't found
in a subclass so it searches the classles) that that subclass inherits
,
from this process ,
-
class Developer (Employee) :
Python uses method resolution order to
der-1
L
eloper (An' , "Tran' , 400000) Developer first (doesn't find anything) ,
then
VERY USEFUL use this line , see wasn't found there Python would
method resolution order (chain of inheritance S
L
check the built in objects class
-
, , subclass
, ,
, ,
language
class Manager (Employee) : -a
list of people supervised by each
manager
(instance)
lef--init--(self, first last , , pay-yees
None) = :
-
a subclass of the parent (Employee) class that has a list of
people managed by each
can also
print the full names of
everyone on the employees list because of inheritance and
a method
-
didn't pass an empty list for employees as the --init-- func. input because
a list is a mutable data type (MDT) mutable data types -
lists +dictionaries + sets
any changes made (things added or removed from the MDT) will change the original copy of
the MDT as Python doesn't make copies of MDTs when they're passed in as arguments ,
the
changes made will affect the MDT outside the method too
built-in functions
instance of class
- (True/false)
isinstance -
checks if an
object is an a both return Boolean
a is a
syntax :
is instance
(myr-1 Manager)
,
issubclass (Developer Employee)
-
checks of mgr-1 is an -
repr - def--repr--Iself) :
def--repr--(self) :
5)
return "Employee (23 %E3:E3') · format (self first, self last self
.
.
,
,
pay)
- T -
placeholders placeholders of first last , pay
-
return 'E3-531 ·
self-email
format (self-full name() ,
- -
placeholders fullname and email
placeholders of
-
returns a readable string repretation of an Employee object (or instance)
will run when you write print (strlemp-1)) or
print lemp-1 --str--
-
.
OUTPUT :
An Tran-AnTran & company .
com
-
-me
add > print (int --add--(1 2) print Istr---add--I'a! 'b') BUILT IN DUNDER METHOD
-
. , ,
&
def--add--self other) ,
:
TO RON :
print (emp-1 .
--add--lemp-2)) OR printladd(emp-1 emp-2)) ,
CONT ,
G
def--len--(self) the
length of instance's
:
- returns an
-
will
print 1 more character than there actually are because it counts
spaces
EXAMPLE:
emp-1 full name
.
=
An Tran
print (emp-1 .
--len--(l) RESULT :
G -
1234567
-ROPERTY
DECORATORS
-
includes getters , setters , deleters , etc.
3
def--init--(self , first,
:
first t + + company
1
selfemail last
: com
but occurs when
problem
.
I
TO FIX THIS
emp-1-first = 'Bob'
emp-1 first
# Bob
=
.
emp-1 .
full name = Bob Tran
make email method structured like fullname
a
similarly
-
emp-1 email
Autrang company
=
.
-
.
com
return 23 [3@
.
email .
com format (self first self last)
- ·
, ,
-
so now to run
you have to add an extra 1) and now
every email attribute is
a method call
TO FIX THIS ISSUE ,
and continue
accessing "email" as an attribute then ...
add decorator
a
property
-
DEFINED By : @ property
-
add a property the line before the method
SO IT WOULD BE
& property
def email (self) :
SETTER EXAMPLE :
-used when
you are
trying to
get first name , last name , + email from
emp-1 fullname .
= An Tran'
On it
def fullname (self, name) : creating another method w/ the same name as the one wy the setter
based
first, last
split (") splitting spaces
= name. = on
first 3
2
self, first =
first name
self last
.
=
last
]-making sure that anything before a space is the
first
a last are defined anything after a space is the last name
for instance
every
DELETER EXAMPLE :
-defined
by doing: propertyname deleter in this .
case > -
&fullname deleter
.
to delete
something do del emp-1 attribute in : this case > def emp-1 full name
-
-
. ·
D
I
& fullname deleter
def fullname (self) :
making another method / the same name as the one
w/theeleter
the deleter
printing something visually
to show that ran
deleted
(setting it as
None)
↑
what gets run when an attribute is deleted
del emp-1 fullname delle
.
point(emp-1 .
fullname) -RESULT : - name None None