What is the setattr method in Python

Python setattr vs. __setattr__ UnicodeEncodeError - python, setattr

I know we have to use method when we are outside of an object. However, I am having trouble calling with unicode key leading me to use it directly.

And I get the following content from:

  • : increases UnicodeEncodeError
  • :
  • :

I don't understand why Python behaves with these differences

Reply:

0 for the answer № 1

Python 2.7? ASCII identifiers only. This includes your code in 2) - but not an ASCII accent.

Unicode identifier in Python?

3) You need to set a unicode key within a dictionary. Legal

Notice that __setattr__ is hardly ever is meant to be used as you do. The intent is to set attributes for an object. Not to be intercepted and packed into an internal dictation attribute. I should also avoid properties as names and confuse them with properties in the get / set sense.

In general, you want to use setattr, not the double underline variant. Contrary to your opening sentence.

Usually you don't too Call With double underscores you define it and the underlying Python data protocol calls it on your behalf. Bit like JavaBeans get / put implicit calls (I think).

__setattr__ can be difficult. If you are not careful, it will block unexpected "cease activity".

Here's a stupid example

Output:

You had to have one after:

Output:

Lastly, if you're just starting out and Unicode is a big deal, I'd evaluate that Python 2 vs 3 - 3 has much better, uniform Unicode support. There are myriad reasons why you may or may not need to use 2.7 instead of 3, but Unicode "pushes" 3.


0 for the answer № 2

Python 2 does not allow Unicode identifiers:

Presumably it's so persistent at this point that you can't work around it because you're trying to go through some processing before calling. You can indicate this by adding an at the very beginning of: nothing will be printed, so the problem is not in your code.