Python setattr() - The Ultimate Guide

preview_player
Показать описание
Python's built-in setattr(object, string, value) function takes three arguments: an object, a string, and an arbitrary value. It sets the attribute given by the string on the object to the specified value.

►► Do you want to thrive as a self-employed Python freelancer controlling your own time, income, and work schedule?

⁉️ Do you have a question? Leave a comment and we will answer as soon as possible!

⏰ Subscribe to the channel, never miss a new video!

🐍 Did you know? Finxter is one of the top 10 Python Blogs on the internet!

🚀 More about Python & Freelancing:

#finxter #python

Do you want to thrive as a self-employed Python freelancer controlling your own time, income, and work schedule? Check out our Python freelancer resources:

Finxter Python Freelancer Course:

Finxter Python Freelancer Webinar:

Leaving the Rat Race with Python (Book):
Рекомендации по теме
Комментарии
Автор

OMG, this is super useful, thank you for the video.

I always thought that the class attributes must be defined in the __init__() method of the class, which I didn't always want to do, especially with classes of dozens of attributes, so I often used a single
dict() called "attributes" instead (like porsche.attributes["speed"] = 100) which was a bit easier to use but still a pain when using nested classes and having stuff like :D This is so much more clever, I wonder why beginner OOP tutorials don't mention it along with the most important concepts.

TomanovicsGergely
Автор

Great explanation! The big question in everyones mind is "Why would I use setattr instead of the dot syntax" :-)

soklagenhet
Автор

Dear Mister Finxter, this was a beautiful delivered vid. Very clear. I would like to read a csv into class objects. The first line of the of the csv is row headers. How do i combine dict to_csv with setattr to have the row headers for the element names of the class? Do you have such an example?

joelboardgamerpger
Автор

Hey Chris, I notice you don't put 'object' in the class defintion, like class Mango(object): // is there a reason? Does it make a difference at all?

zigginzag
Автор

Hi i like to much your content, may be if you like of course, you can make drf o django or any other framework content in the future i will consume much more of your channel. Thaks

packo