Python Projects for Beginners by Connor P. Milliken

Python Projects for Beginners by Connor P. Milliken

Author:Connor P. Milliken
Language: eng
Format: epub
ISBN: 9781484253557
Publisher: Apress


Method Scope

Like global attributes, you may have methods that are accessible through the class itself rather than an instance of the class. These may also be known as static methods . They are not accessible by instances of the class. Depending on the class your building, it may help to have a method that is only accessible through the class and not the instances. Let’s see an example: 1| # understanding which methods are accessible via the class itself and class instances

3| class Dog( ):

4| sound = "bark"

6| def makeSound(self):

7| print(self.sound)

9| def printInfo( ):

10| print("I am a dog.")

12| Dog.printInfo( ) # able to run printInfo method because it does not include self parameter

14| # Dog.makeSound( ) would produce error, self is in reference to instances only

16| sam = Dog( )

18| sam.makeSound( ) # able to access, self can reference the instance of sam

20| # sam.printInfo( ) will produce error, instances require the self parameter to access methods



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.