Object-Oriented Programming I - NASA

Transcription

Object-Oriented Programming I:Classes, Attributes, Methods, and Instances

Brief Outline#0

Brief Outline What is object-oriented programming?#0

Brief Outline What is object-oriented programming? How do I implement it in Python?#0

Brief Outline What is object-oriented programming? How do I implement it in Python? Basic examples#0

Procedural Programmingfunction1(var1, var2, etc.) function2(var3,var4,etc.) function3(var5,var6,etc.) . Final Product#1

Procedural Programming This has been the mainstay of much scientificprogramming, and it works well. But it can get very messy when you have acomplex program with lots of interactingparts Particularly when data has to be shared andmodified between many functions!#2

What is Object-OrientedProgramming?#3

What is Object-OrientedProgramming?Answer 1a: Ask an expert#3

What is Object-OrientedProgramming?Answer 1a: Ask an expert#3

What is Object-OrientedProgramming?#4

What is Object-OrientedProgramming?Answer Ib: Ask an expert Wikipedia#4

What is Object-OrientedProgramming?Object-oriented programming (OOP) is aprogramming paradigm that uses "objects" –data structures consisting of data fields andmethods together with their interactions – todesign applications and computer programs.Programming techniques may include featuressuch as data abstraction, encapsulation,modularity, polymorphism, and inheritance.Answer Ib: Ask an expert Wikipedia#4

What is Object-OrientedProgramming?#5

What is Object-OrientedProgramming?Objects are like animals: they know how to do stuff (like eatand sleep), they know how to interact with others (like makechildren), and they have characteristics (like height, weight).#5

What is Object-OrientedProgramming?Objects are like animals: they know how to do stuff (like eatand sleep), they know how to interact with others (like makechildren), and they have characteristics (like height, weight).#5

What is Object-OrientedProgramming?CharacteristicsColor, Height, WeightObjects are like animals: they know how to do stuff (like eatand sleep), they know how to interact with others (like makechildren), and they have characteristics (like height, weight).#5

What is Object-OrientedProgramming?CharacteristicsColor, Height, WeightDoes ThingsEat, Sleep, Growl, CheerObjects are like animals: they know how to do stuff (like eatand sleep), they know how to interact with others (like makechildren), and they have characteristics (like height, weight).#5

What is Object-OrientedProgramming?CharacteristicsColor, Height, WeightDoes ThingsEat, Sleep, Growl, CheerInteractionParents, siblings, friendsObjects are like animals: they know how to do stuff (like eatand sleep), they know how to interact with others (like makechildren), and they have characteristics (like height, weight).#5

What is Object-OrientedProgramming?#6

What is Object-OrientedProgramming?An object is a programming structure that allows you togroup together variables (characteristics) and functions (doingthings) in one nice, tidy package. In Python, the blueprint foran object is referred to as a class.#6

What is Object-OrientedProgramming?BearVariables: color, height, weightFunctions: eat, sleep, growlAn object is a programming structure that allows you togroup together variables (characteristics) and functions (doingthings) in one nice, tidy package. In Python, the blueprint foran object is referred to as a class.#6

What is Object-OrientedProgramming?BearAttributes: color, height, weightMethods: eat, sleep, growlWithin a class, the variables are referred to as attributes andthe functions are referred to as methods.#7

What is Object-OrientedProgramming?Instances are specific realizations of a class#8

What is Object-OrientedProgramming?YogiAttributes: brown, 1.8 m, 80 kgMethods: eat, sleep, growlInstances are specific realizations of a class#8

What is Object-OrientedProgramming?YogiWinnieAttributes: brown, 1.8 m, 80 kgAttributes: yellow, 1.2 m, 100 kgMethods: eat, sleep, growlMethods: eat, sleep, growlInstances are specific realizations of a class#8

Object Syntax in Python#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2]#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].[Variable1] # “Global” class variables can be defined here#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].[Variable1] # “Global” class variables can be defined here#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].[Variable1] # “Global” class variables can be defined heredef Method1(self, args, kwargs {}):#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].[Variable1] # “Global” class variables can be defined heredef Method1(self, args, kwargs {}):# Performs task 1#9

Object Syntax in Pythonclass ClassName[(BaseClasses)]:“””[Documentation String]”””[Statement1] # Executed only when class is defined[Statement2].[Variable1] # “Global” class variables can be defined heredef Method1(self, args, kwargs {}):# Performs task 1#9

Bear: Our first Python class#10

Bear: Our first Python class class Bear:!!!!!!!!!!#10

Bear: Our first Python class class Bear:!!!!!!!!!!We are defining a newclass named Bear.Note the lack ofparentheses. These areonly used if the class isderived from otherclasses (more on thisnext lecture).#10

Bear: Our first Python class class Bear:#10

Bear: Our first Python class class Bear:.print "The bear class is now defined."#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".This print statement isexecuted only whenthe class is defined.#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined.This print statement isexecuted only whenthe class is defined.#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined.#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a BearThis statement equatesthe object a to theclass Bear. This istypically not veryuseful.#10

Bear: Our first Python class .The class Bear:print "The bear class is now defined."bear class is now defined.a BearaThis statement equatesthe object a to theclass Bear. This istypically not veryuseful.#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 This statement equatesthe object a to theclass Bear. This istypically not veryuseful.#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 #10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 a Bear()#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 a Bear() a#10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 a Bear() a main .Bear instance at 0x100433cb0 #10

Bear: Our first Python class class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear a class main .Bear at 0x10041d9b0 a Bear() a main .Bear instance at 0x100433cb0 By adding parenthesis,we are creating a newinstance of the classBear.#10

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear()#11

Attributes: Access, Creation,Deletion .The class Bear:print "The bear class is now defined."bear class is now defined.a Bear()a.name#11

Attributes: Access, Creation,Deletion .The class Bear:print "The bear class is now defined."bear class is now defined.a Bear()a.nameObject attributes areaccessed with the“.” (period) operator#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):Object attributes areaccessed with the“.” (period) operator#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module Object attributes areaccessed with the“.” (period) operator#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name'Object attributes areaccessed with the“.” (period) operator#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name'#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name'(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski"(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown"(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown" del(a.name)(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown" del(a.name) a.name(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown" del(a.name) a.nameTraceback (most recent call last):(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown" del(a.name) a.nameTraceback (most recent call last):File " stdin ", line 1, in module (Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Attributes: Access, Creation,Deletion class Bear:.print "The bear class is now defined.".The bear class is now defined. a Bear() a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name' a.name "Oski" a.color "Brown" del(a.name) a.nameTraceback (most recent call last):File " stdin ", line 1, in module AttributeError: Bear instance has no attribute 'name'(Instance-specific)attributes can becreated and deletedoutside of the classdefinition#11

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined."Methods are defined inthe same way normalfunctions are (notethat we will return tothe self object in a fewslides)#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):Methods are defined inthe same way normalfunctions are (notethat we will return tothe self object in a fewslides)#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear."Methods are defined inthe same way normalfunctions are (notethat we will return tothe self object in a fewslides)#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".Methods are defined inthe same way normalfunctions are (notethat we will return tothe self object in a fewslides)#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".The bear class is now defined.Methods are defined inthe same way normalfunctions are (notethat we will return tothe self object in a fewslides)#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".The bear class is now defined. a Bear()Like attributes,methods are alsoaccessed via the “.”operator. Parenthesesindicate the methodshould be executed.#12

Methods: Access, Creation, and(not) Deletion .The class Bear:print "The bear class is now defined."def say hello(self):print "Hello, world! I am a bear."bear class is now defined.a Bear()a.say helloLike attributes,methods are alsoaccessed via the “.”operator. Parenthesesindicate the methodshould be executed.#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".The bear class is now defined. a Bear() a.say hello bound method Bear.say hello of main .Bearinstance at 0x100433e18 Like attributes,methods are alsoaccessed via the “.”operator. Parenthesesindicate the methodshould be executed.#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".The bear class is now defined. a Bear() a.say hello bound method Bear.say hello of main .Bearinstance at 0x100433e18 a.say hello()Like attributes,methods are alsoaccessed via the “.”operator. Parenthesesindicate the methodshould be executed.#12

Methods: Access, Creation, and(not) Deletion class Bear:.print "The bear class is now defined.".def say hello(self):.print "Hello, world! I am a bear.".The bear class is now defined. a Bear() a.say hello bound method Bear.say hello of main .Bearinstance at 0x100433e18 a.say hello()Hello, world! I am a bear.Like attributes,methods are alsoaccessed via the “.”operator. Parenthesesindicate the methodshould be executed.#12

The init method class Bear:init is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):init is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name nameinit is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):init is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear."init is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.nameinit is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.init is a specialPython method. It isalways run when a newinstance of a class iscreated.#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name. a Bear()Arguments specifiedby init must beprovided whencreating a new instanceof a class (else anException will bethrown)#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name. a Bear()Traceback (most recent call last):Arguments specifiedby init must beprovided whencreating a new instanceof a class (else anException will bethrown)#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name. a Bear()Traceback (most recent call last):File " stdin ", line 1, in module Arguments specifiedby init must beprovided whencreating a new instanceof a class (else anException will bethrown)#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name. a Bear()Traceback (most recent call last):File " stdin ", line 1, in module TypeError: init () takes exactly 2arguments (1 given)Arguments specifiedby init must beprovided whencreating a new instanceof a class (else anException will bethrown)#13

The init method class Bear:.def init (self, name):.self.name name.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name. a Bear()Traceback (most recent call last):File " stdin ", line 1, in module TypeError: init () takes exactly 2arguments (1 given) a Bear(“Yogi”)Arguments specifiedby init must beprovided whencreating a new instanceof a class (else anException will bethrown)#13

Scope: self and “class” variables#14

Scope: self and “class” variables class Bear:#14

Scope: self and “class” variables class Bear:Class-wide(“global”)attributes can bedeclared. It isgood style to dothis before theinit method.#14

Scope: self and “class” variables class Bear:.population 0Class-wide(“global”)attributes can bedeclared. It isgood style to dothis before theinit method.#14

Scope: self and “class” variables class Bear:.population 0#14

Scope: self and “class” variables class Bear:.population 0They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name nameThey are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear."They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.nameThey are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.populationThey are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population.They are accessedin the same way as“instance-specific”attributes, butusing the classname instead ofthe instance name.#14

Scope: self and “class” variables class Bear:!.population 0!.def init (self, name):!.self.name name!.Bear.population 1!.def say hello(self):!.print "Hello, world! I am a bear."!.print “My name is %s.” % self.name!.print “I am number %i.” % Bear.population!.!!!!!!!!!!The self variable isa placeholder forthe specificinstance of a class.Attributesreferenced to selfare known as“object” attributes.#14

Scope: self and “class” variables class Bear:!.population 0!.def init (self, name):!.self.name name!.Bear.population 1!.def say hello(self):!.print "Hello, world! I am a bear."!.print “My name is %s.” % self.name!.print “I am number %i.” % Bear.population!.!!!!!!!!!!It should be listedas a requiredargument in allclass methods(even if it is notexplicitly used bythe method).#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population.When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”)When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.I am number 1.When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.I am number 1. b Bear("Winnie")When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.I am number 1. b Bear("Winnie") b.say hello()When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.I am number 1. b Bear("Winnie") b.say hello()Hello, world! I am a bear.When calling amethod directlyfrom a specificinstance of a class,the self variable isNOT passed(Python handlesthis for you)#14

Scope: self and “class” variables class Bear:.population 0.def init (self, name):.self.name name.Bear.population 1.def say hello(self):.print "Hello, world! I am a bear.".print “My name is %s.” % self.name.print “I am number %i.” % Bear.population. a Bear(“Yogi”) a.say hello()Hello, world! I am a bear.My name is Yogi.I am number 1. b Bear("Winnie") b.say hello()Hello, world! I am a bear.My name is Winnie.When calling amethod directl

What is Object-Oriented Programming? An object is a programming structure that allows you to group together variables (characteristics) and functions (doing things) in one nice, tidy package. In Python, the blueprint for an object is referred to as a class. Bear Variabl