Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

4.3 Ενέργειες επι των κλάσεων

Το παράδειγμα που ακολουθεί δείχνει πως να χρησιμοποιηθεί το self σαν παράμετρος στη μέθοδο __init__() της κλάσης Person:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
class Person: def __init__(self, name, age): self.name = name self.age = age def myfunc(self): print("Hello my name is " + self.name) p1 = Person("John", 36) p1.myfunc()
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()

Στο παράδειγμα αυτό, το self χρησιμοποιείται ως παράμετρος στην __init__() μέθοδο για να αναφερθεί στο αντικείμενο της κλάσης Person που θα δημιουργηθεί. Τα ονόματα name και age ανατίθενται στο αντικείμενο χρησιμοποιώντας το self. Στη συνέχεια, η μέθοδος myfunc() χρησιμοποιεί το self για να αναφερθεί στο αντικείμενο και να εκτυπώσει το όνομα του αντικειμένου Person.

Στο παρακάτω παράδειγμα θα δείξουμε πως μπορούμε να τροποποιήσουμε μια ιδιότητα ενός αντικειμένου της κλάσης Person:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.age = 40
print(p1.age)
class Person: def __init__(self, name, age): self.name = name self.age = age def myfunc(self): print("Hello my name is " + self.name) p1 = Person("John", 36) p1.age = 40 print(p1.age)
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)

p1.age = 40

print(p1.age)

Στο παραπάνω παράδειγμα, αλλάξαμε την ιδιότητα age του αντικειμένου p1 από 36 σε 40 και την εκτυπώσαμε στην οθόνη.

[adinserter block=”2″]

Μπορείτε να διαγράψετε ιδιότητες σε αντικείμενα χρησιμοποιώντας τη λέξη-κλειδί del:

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")
# Create an object
person = Person("John", 36)
# Access object properties
print(person.name)
print(person.age)
# Modify object properties
person.age = 40
# Access modified object property
print(person.age)
# Delete object properties
del person.age
class Person: def __init__(self, name, age): self.name = name self.age = age def greet(self): print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.") # Create an object person = Person("John", 36) # Access object properties print(person.name) print(person.age) # Modify object properties person.age = 40 # Access modified object property print(person.age) # Delete object properties del person.age
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def greet(self):
    print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")

# Create an object
person = Person("John", 36)

# Access object properties
print(person.name)
print(person.age)

# Modify object properties
person.age = 40

# Access modified object property
print(person.age)

# Delete object properties
del person.age

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
John
36
40
John 36 40
John
36
40

Μπορείτε να διαγράψετε ένα αντικείμενο χρησιμοποιώντας τη λέξη-κλειδί del. Αυτό θα διαγράψει την αναφορά στο αντικείμενο, και το αντικείμενο θα καταστεί μη προσβάσιμο.

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
car1 = Car("Ford", "Mustang", 1964)
car2 = Car("Tesla", "Model S", 2021)
del car1
class Car: def __init__(self, brand, model, year): self.brand = brand self.model = model self.year = year car1 = Car("Ford", "Mustang", 1964) car2 = Car("Tesla", "Model S", 2021) del car1
class Car:
  def __init__(self, brand, model, year):
    self.brand = brand
    self.model = model
    self.year = year

car1 = Car("Ford", "Mustang", 1964)
car2 = Car("Tesla", "Model S", 2021)

del car1

Στο παραπάνω παράδειγμα, δημιουργούμε δύο αντικείμενα της κλάσης Car, car1 και car2. Στη συνέχεια, χρησιμοποιούμε την del για να διαγράψουμε το αντικείμενο car1.

Οι ορισμοί κλάσεων δεν μπορούν να είναι κενοί, αλλά αν για κάποιο λόγο έχετε έναν ορισμό κλάσης χωρίς περιεχόμενο, χρησιμοποιήστε τη δήλωση pass για να αποφευχθεί ο σφάλματος.

Παράδειγμα μιας κλάσης με τη χρήση της εντολής pass σε περίπτωση που η κλάση είναι κενή:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class MyClass:
pass
class MyClass: pass
class MyClass:
  pass

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων