வகுப்பு பண்புகள்
பண்புகள் என்பது ஒரு வகுப்பைச் சேர்ந்த மாறிகள். அவை வகுப்பிலிருந்து உருவாக்கப்பட்ட ஒவ்வொரு பொருளுக்கும் தரவைச் சேமிக்கின்றன.
எடுத்துக்காட்டு
பண்புகளுடன் ஒரு வகுப்பை உருவாக்கவும்:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Emil", 36)
print(p1.name)
print(p1.age)
36
பண்புகளை அணுகுதல்
புள்ளி குறியீட்டைப் பயன்படுத்தி பொருள் பண்புகளை அணுகலாம்:
அணுகல் தொடரியல்
பொருள் பண்புகளை அணுகுவதற்கான சரியான தொடரியல்:
எடுத்துக்காட்டு
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.brand)
print(car1.model)
பண்புகளை மாற்றுதல்
பொருள்களில் உள்ள பண்புகளின் மதிப்பை மாற்றலாம்:
எடுத்துக்காட்டு
வயது பண்பை மாற்றவும்:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Tobias", 25)
print("Original age:", p1.age)
p1.age = 26
print("Modified age:", p1.age)
Modified age: 26
பண்புகளை நீக்குதல்
del விசைச் சொல்லைப் பயன்படுத்தி பொருள்களிலிருந்து பண்புகளை நீக்கலாம்:
எடுத்துக்காட்டு
வயது பண்பை நீக்கவும்:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Linus", 30)
del p1.age
print(p1.name) # இது வேலை செய்யும்
# print(p1.age) # இது பிழையை ஏற்படுத்தும்
எச்சரிக்கை:
நீக்கப்பட்ட பண்பை அணுக முயற்சித்தால், AttributeError ஏற்படும். பண்புகளை நீக்குவதற்கு முன் சரியாக என்ன செய்கிறீர்கள் என்பதை உறுதிப்படுத்திக் கொள்ளுங்கள்.
வகுப்பு பண்புகள் vs பொருள் பண்புகள்
__init__() உள்ளே வரையறுக்கப்பட்ட பண்புகள் ஒவ்வொரு பொருளுக்கும் சொந்தமானவை (உதாரணம் பண்புகள்).
முறைகளுக்கு வெளியே வரையறுக்கப்பட்ட பண்புகள் வகுப்புக்கே சொந்தமானவை (வகுப்பு பண்புகள்) மற்றும் அனைத்து பொருள்களாலும் பகிரப்படுகின்றன:
உதாரணம் பண்புகள்
class Person:
def __init__(self, name):
self.name = name # உதாரணம் பண்பு
p1 = Person("Emil")
p2 = Person("Tobias")
p1.name = "John" # p1 மட்டும் மாற்றம்
print(p1.name) # John
print(p2.name) # Tobias
• ஒவ்வொரு பொருளுக்கும் தனித்தனியாக
• ஒரு பொருளில் மாற்றம் மற்ற பொருள்களை பாதிக்காது
வகுப்பு பண்புகள்
class Person:
species = "Human" # வகுப்பு பண்பு
def __init__(self, name):
self.name = name
p1 = Person("Emil")
p2 = Person("Tobias")
Person.species = "Homo Sapiens"
print(p1.species) # Homo Sapiens
print(p2.species) # Homo Sapiens
• அனைத்து பொருள்களுக்கும் பொதுவானது
• வகுப்பு மட்டத்தில் மாற்றம் அனைத்து பொருள்களையும் பாதிக்கிறது
முழு எடுத்துக்காட்டு
class Person:
species = "Human" # வகுப்பு பண்பு
def __init__(self, name):
self.name = name # உதாரணம் பண்பு
p1 = Person("Emil")
p2 = Person("Tobias")
print(p1.name) # Emil
print(p2.name) # Tobias
print(p1.species) # Human
print(p2.species) # Human
# வகுப்பு பண்பை மாற்றுதல்
Person.species = "Homo Sapiens"
print("After change:")
print(p1.species) # Homo Sapiens
print(p2.species) # Homo Sapiens
Tobias
Human
Human
After change:
Homo Sapiens
Homo Sapiens
புதிய பண்புகளைச் சேர்த்தல்
ஏற்கனவே உள்ள பொருள்களுக்கு புதிய பண்புகளைச் சேர்க்கலாம்:
எடுத்துக்காட்டு
ஒரு பொருளுக்கு புதிய பண்புகளைச் சேர்க்கவும்:
class Person:
def __init__(self, name):
self.name = name
p1 = Person("Tobias")
# புதிய பண்புகளைச் சேர்த்தல்
p1.age = 25
p1.city = "Oslo"
print(p1.name)
print(p1.age)
print(p1.city)
25
Oslo
முக்கியமான குறிப்பு:
இந்த வழியில் பண்புகளைச் சேர்ப்பது அந்த குறிப்பிட்ட பொருளுக்கு மட்டுமே அவற்றைச் சேர்க்கிறது, வகுப்பின் அனைத்து பொருள்களுக்கும் அல்ல. மற்ற பொருள்கள் இந்த புதிய பண்புகளைக் கொண்டிருக்காது.