“Zajęcia w Python” Kod odpowiedzi

Jak zrobić zajęcia w 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

Klasa 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

Klasa 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

Zajęcia w Python

class Foo:
  def __init__(self):
    self.definition = Foo!
  def hi():
    # Some other code here :)
    
# Classes require an __init__ if you want to assign attributes. (self) defines what describes the attribs.
      
AcaiBerii

Zajęcia w Python

class fruit:
  def __init__(self,color,taste,name):
    self.color = color
    self.name = name
    self.taste = taste
    
    def myfunc(self):
      print("{} = Taste:{}, Color:{}".format(self.name, self.taste, self.color))
      
f1 = fruit("Red", "Sweet", "Red Apple")
f1.myfunc()
Fantastic Fly

Zajęcia w Python

>>> print(ObjectCreator) # you can print a class because it's an object
<class '__main__.ObjectCreator'>
>>> def echo(o):
...       print(o)
...
>>> echo(ObjectCreator) # you can pass a class as a parameter
<class '__main__.ObjectCreator'>
>>> print(hasattr(ObjectCreator, 'new_attribute'))
False
>>> ObjectCreator.new_attribute = 'foo' # you can add attributes to a class
>>> print(hasattr(ObjectCreator, 'new_attribute'))
True
>>> print(ObjectCreator.new_attribute)
foo
>>> ObjectCreatorMirror = ObjectCreator # you can assign a class to a variable
>>> print(ObjectCreatorMirror.new_attribute)
foo
>>> print(ObjectCreatorMirror())
<__main__.ObjectCreator object at 0x8997b4c>
Said HR

Odpowiedzi podobne do “Zajęcia w Python”

Pytania podobne do “Zajęcia w Python”

Więcej pokrewnych odpowiedzi na “Zajęcia w Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu