3
3
3Create2020-06-14 12:01:57
Python
3Create, 2020-06-14 12:01:57

How to correctly compose a class with a self argument?

What am I doing wrong?

class Person:
    name = "Кристи"
    age = 10

    def set(self, name, age):
        self.name = name
        self.age = age


lena = Person()
lena.set = ("Лена", 25)
print(lena.name + " " + str(lena.age))

ivan = Person()
ivan.set = ("Иван", 33)
print(ivan.name + " " + str(ivan.age))

The print only outputs this:
Кристи 10
Кристи 10

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-06-14
@3Create

Your set is a function. Accordingly , arguments can also be passed directly when creating an instance of the class
ivan.set("Иван", 33)

class Person:
    name = "Кристи"
    age = 10

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def set(self, name, age):
        self.name = name
        self.age = age

ivan = Person('Иван', 33)

G
galaxy, 2020-06-14
@galaxy

lena.set("Лена", 25)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question