Python Classes and Objects [With Examples] - Programiz
文章推薦指數: 80 %
Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword. The first string inside the class is called ...
CourseIndex
ExploreProgramiz
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
LearnPythonpractically
andGetCertified.
ENROLL
PopularTutorials
GettingStartedWithPython
PythonifStatement
whileLoopinPython
PythonLists
DictionariesinPython
StartLearningPython
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
ExplorePythonExamples
ReferenceMaterials
Built-inFunctions
ListMethods
DictionaryMethods
StringMethods
Viewall
LearningPaths
Challenges
LearnPythonInteractively
TryforFree
Courses
BecomeaPythonMaster
BecomeaCMaster
BecomeaJavaMaster
ViewallCourses
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
LearnPythonpractically
andGetCertified.
ENROLLFORFREE!
PopularTutorials
GettingStartedWithPython
PythonifStatement
whileLoopinPython
PythonLists
DictionariesinPython
StartLearningPython
AllPythonTutorials
ReferenceMaterials
Built-inFunctions
ListMethods
DictionaryMethods
StringMethods
Viewall
Python
JavaScript
C
C++
Java
Kotlin
LearnPythonpractically
andGetCertified.
ENROLLFORFREE!
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
AllPythonExamples
LearnPythonInteractively
PythonIntroduction
GettingStarted
KeywordsandIdentifier
Statements&Comments
PythonVariables
PythonDataTypes
PythonTypeConversion
PythonI/OandImport
PythonOperators
PythonNamespace
PythonFlowControl
Pythonif...else
PythonforLoop
PythonwhileLoop
Pythonbreakandcontinue
PythonPass
PythonFunctions
PythonFunction
FunctionArgument
PythonRecursion
AnonymousFunction
Global,LocalandNonlocal
PythonGlobalKeyword
PythonModules
PythonPackage
PythonDatatypes
PythonNumbers
PythonList
PythonTuple
PythonString
PythonSet
PythonDictionary
PythonFiles
PythonFileOperation
PythonDirectory
PythonException
ExceptionHandling
User-definedException
PythonObject&Class
PythonOOP
PythonClass
PythonInheritance
MultipleInheritance
OperatorOverloading
PythonAdvancedTopics
PythonIterator
PythonGenerator
PythonClosure
PythonDecorators
PythonProperty
PythonRegEx
PythonExamples
PythonDateandtime
PythondatetimeModule
Pythondatetime.strftime()
Pythondatetime.strptime()
Currentdate&time
Getcurrenttime
Timestamptodatetime
PythontimeModule
Pythontime.sleep()
RelatedTopics
PythonObjectOrientedProgramming
Pythongetattr()
Pythonhasattr()
PythonFunctionArguments
Pythonsetattr()
PythonDocstrings
PythonObjectsandClasses
Inthistutorial,youwilllearnaboutthecorefunctionalityofPythonobjectsandclasses.You'lllearnwhataclassis,howtocreateitanduseitinyourprogram.
Video:PythonClassesandObjects
PythonObjectsandClasses
Pythonisanobject-orientedprogramminglanguage.Unlikeprocedure-orientedprogramming,wherethemainemphasisisonfunctions,object-orientedprogrammingstressesonobjects.
Anobjectissimplyacollectionofdata(variables)andmethods(functions)thatactonthosedata.Similarly,aclassisablueprintforthatobject.
Wecanthinkofaclassasasketch(prototype)ofahouse.Itcontainsallthedetailsaboutthefloors,doors,windows,etc.Basedonthesedescriptionswebuildthehouse.Houseistheobject.
Asmanyhousescanbemadefromahouse'sblueprint,wecancreatemanyobjectsfromaclass.Anobjectisalsocalledaninstanceofaclassandtheprocessofcreatingthisobjectiscalledinstantiation.
DefiningaClassinPython
LikefunctiondefinitionsbeginwiththedefkeywordinPython,classdefinitionsbeginwithaclasskeyword.
Thefirststringinsidetheclassiscalleddocstringandhasabriefdescriptionoftheclass.Althoughnotmandatory,thisishighlyrecommended.
Hereisasimpleclassdefinition.
classMyNewClass:
'''Thisisadocstring.Ihavecreatedanewclass'''
pass
Aclasscreatesanewlocalnamespacewhereallitsattributesaredefined.Attributesmaybedataorfunctions.
Therearealsospecialattributesinitthatbeginswithdoubleunderscores__.Forexample,__doc__givesusthedocstringofthatclass.
Assoonaswedefineaclass,anewclassobjectiscreatedwiththesamename.Thisclassobjectallowsustoaccessthedifferentattributesaswellastoinstantiatenewobjectsofthatclass.
classPerson:
"Thisisapersonclass"
age=10
defgreet(self):
print('Hello')
#Output:10
print(Person.age)
#Output:
延伸文章資訊
- 1Python Classes and Objects [With Examples] - Programiz
Like function definitions begin with the def keyword in Python, class definitions begin with a cl...
- 2Python Classes/Objects - W3Schools
Python is an object oriented programming language. Almost everything in Python is an object, with...
- 39. Classes — Python 3.10.7 documentation
Classes provide a means of bundling data and functionality together. Creating a new class creates...
- 4Python Classes and Objects - GeeksforGeeks
Python Classes and Objects ... A class is a user-defined blueprint or prototype from which object...
- 5[Python物件導向]淺談Python類別(Class) - Learn Code With Mike
定義方法(Method)和函式(Function)的語法很像,都是def關鍵字開頭,接著自訂名稱,但是方法(Method)和建構式(Constructor)一樣至少要有一個self參數,語法如下...