“Classes et objets Python” Réponses codées

Comment faire une classe en Python

class Person:
  def __init__(self, _name, _age):
    self.name = _name
    self.age = _age
   
  def sayHi(self):
    print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
    
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
Red Tailed Cockatoo

classe python

class Person:#set name of class to call it 
  def __init__(self, name, age):#func set ver
    self.name = name#set name
    self.age = age#set age
   

    def myfunc(self):#func inside of class 
      print("Hello my name is " + self.name)# code that the func dose

p1 = Person("barry", 50)# setting a ver fo rthe class 
p1.myfunc() #call the func and whitch ver you want it to be with 
rej

classe python

# Standard way of writing a simple class
class Person1:
# Type hinting not required
    def __init__(self, name: str, age: int, num_children=0):
        self.name = name
        self.age = age
        self.num_children = num_children

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

      
from dataclasses import dataclass


# A class using data classes. Dataclasses are simpler but can't support operations during initialization
@dataclass()
class Person2:
    """ This class handles the values related to a person. """
    name: str  # Indicating types is required
    age: int
    num_children = 0  # Default values don't require an indication of a type

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

# Both classes (Person1 and Person2) achieve the same thing but require different code to do it

person1 = Person1('Joe', 28, 2)
print(person1)
# Result: My name is Joe, I am 28 years old, and I have 2 children

person2 = Person2('Emma', 19)
print(person2)
# Result: My name is Emma, I am 19 years old, and I have 0 children
YEP Python

Créer une classe et un objet dans Python

class Parrot:

    # class attribute
    species = "bird"

    # instance attribute
    def __init__(self, name, age):
        self.name = name
        self.age = age

# instantiate the Parrot class
blu = Parrot("Blu", 10)
woo = Parrot("Woo", 15)

# access the class attributes
print("Blu is a {}".format(blu.__class__.species))
print("Woo is also a {}".format(woo.__class__.species))

# access the instance attributes
print("{} is {} years old".format( blu.name, blu.age))
print("{} is {} years old".format( woo.name, woo.age))
Lenka Skalová

classe python

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("Victor", 24)
p1.myfunc()
Blushing Badger

Classes et objets Python

class Person:
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    def greet(self, person_to_greet):
        # person_to_greet will be another Person object
        print(f"Hey {person_to_greet.name}, nice to meet you I'm {self.name}")

    def ask_age(self, ask_from):
        print(f"{self.name}: {ask_from.name}, How old are you?")
        print(f"{ask_from.name}: i am {ask_from.age}")


# Creating a person object
tom = Person("Tom", 50, "Male")

# we can also create an object with keyword arguments
jack = Person(name="Jack", age=19, gender="Male")

# Here we call the greet method of tom, and we pass the Jack Person Object Created above
tom.greet(jack)

# if we call the greet method of jack and pass the Tom person object, then jack greets tom
jack.greet(tom)

# Here Jack will ask age of tom, and tom will reply with his age
jack.ask_age(tom)
Defeated Deer

Réponses similaires à “Classes et objets Python”

Questions similaires à “Classes et objets Python”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code