Python: Assign value to non-existing field [duplicate] - python

This question already has answers here:
How can I create an object and add attributes to it?
(18 answers)
How do I create a Python namespace (argparse.parse_args value)?
(4 answers)
Closed 3 years ago.
a = None
a.s = 2
throws an AttributeError. Is there a possibility to create an object a with the field s with value 2 without creating an own class for that? Would be super cool if Python would allow that hack! 8)

Related

How to apply a list of attributes to an object in Python [duplicate]

This question already has answers here:
How to access (get or set) object attribute given string corresponding to name of that attribute
(3 answers)
Closed 1 year ago.
I have a list of attributes for an object. I will like to loop over the list and apply them to that object.
Here is what I did:
attrs = ['ClearNoon',
'ClearSunset',
'CloudyNoon',
'CloudySunset',
'HardRainNoon',
'HardRainSunset',
'MidRainSunset',
'MidRainyNoon',
'SoftRainNoon',
'SoftRainSunset',
'WetCloudyNoon',
'WetCloudySunset',
'WetNoon',
'WetSunset']
for attr in attrs:
world.set_weather(carla.WeatherParameters.attr)
Unfortunately, this does not work. Any better suggestions please?
Try using getattr:
world.set_weather(getattr(carla.WeatherParameters, attr))

Why does python modify list in tuple AND throw an exception [duplicate]

This question already has answers here:
Append to a list defined in a tuple - is it a bug? [duplicate]
(4 answers)
a mutable type inside an immutable container
(3 answers)
Closed 4 years ago.
The question is pretty simple, suppose you have the following code:
a = (5,6,[7,8])
a[2] += [8]
It raises an exception as would be predicted, because tuples are immutable, but
print(a) results in (5,6,[7,8,9]) I wanted to figure out what is going on behind the scenes.
Alternatively if you use extend on a[2] the exception is not thrown.

get variable name of instance [duplicate]

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 8 years ago.
Can a Python instance get the name of the variable which is used to access the object?
This example code shows what I need:
foo=MyClass()
foo.get_name() --> 'foo'
bar=foo
bar.get_name() --> 'bar'
I know that this is black magic and not clean code. I just want to know if it is possible.
I know that bar.__name__ returns the name, but I need it inside an own method.
How can get_name() be implemented?
This is not a duplicate of questions which answer is __name__

How can i convert a string in a python method? [duplicate]

This question already has answers here:
Calling a function of a module by using its name (a string)
(18 answers)
Closed 8 years ago.
I receive a string, for example "save". And i have a method save with paramethers.
How can i convert the string save in a call to save().
I tried with eval and exec.
Edit: Solved here --> Calling a function of a module from a string with the function's name in Python
def some_method(self):
save_method = getattr(self, 'save')
save_method() # save()

Python private class fields, how to make? [duplicate]

This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 9 years ago.
I'm beginner. In Delphi I can make private field in class: moving it to private section. So ClassVar.Field doesn't exist out of class.
In Python, if I make class
class CName:
testname = 10
then testname can be accessed always. How can I make a field "private"?
You can't!
Naming fields with two underscores will hide it (generate a new name) but you will always be able to access it. Try __testname = 10

Categories