Python set object name

Python __set_name__() Magic Method

Be on the Right Side of Change

Python’s magic method obj.__set_name__(self, owner, name) method is created on an attribute obj when the class owner holding the attribute is created.

We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

Syntax and Minimal Example

object.__set_name__(self, owner, name)
class Attribute: pass class My_Class: x = Attribute() # Python calls: x.__set_name__(My_Class, 'x')

The expression x = Attribute() internally causes Python to call x.__set_name__(My_Class, ‘x’) .

Overriding __set_name__() Example

Let’s override the __set_name__ magic method on the Attribute ‘s class:

class Attribute: def __set_name__(self, owner, name): print('Python is great!') class My_Class: x = Attribute()

In fact, the magic method is called which results in the output:

More Practical Example

The purpose of the magic method __set_name__() , however, is not to print dummy strings to the Python shell—as you may have guessed already.

💡 Note: The __set_name__() method is called automatically by Python for every single attribute held by the owner class object when initializing an object—in our previous example an object of type My_Class . More details here.

However, the __set_name__() method is not called when assigning attributes to an object dynamically—later in the code.

To see what I mean, have a look at this code snippet:

class Attribute: def __set_name__(self, owner, name): print(‘Python is great!’) class My_Class: pass My_Class.x = Attribute() # x.__set_name__() is NOT called!! #

Now, you can manually call the __set_name__() method to run the same routine you’d have run if you’d initialized the attribute in the class definition right away:

class Attribute: def __set_name__(self, owner, name): print('Python is great!') class My_Class: pass My_Class.x = Attribute() # My_Class.x.__set_name__(My_Class, 'x') # Python is great!

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Читайте также:  Moocbeliro ru moodle course index php
Оцените статью