How to set subproperty variable?

I am new to Python. I got Python sample code from a software vendor who expanded their programming API with boost.python, so we can name them in Python. I am confused by some segments, for example:

settings = zoo.AddAnimalSettings(carni_bird_list) settings.Name = 'birds' settings.Type = settings.Type.enum.Bird settings.water_min = 1, units.Litre settings.food_min = 10, units.Gram 

All variable names are replaced with all these funny things, just to explain the general idea.

So, the problem is in the third line. How can we set the variable settings.Type with its additional property settings.Type.enum.Bird , where enum.Bird I assume that this is some kind of enumerator of different species of animals, which is a sub-property of settings.Type ?

I tried to do some kind of test to add one line from the following 5 lines to see if there is enum.Bird :

 settings.Type = settings.Type.enum.Bird 

and it works fine. So, for this instance of settings this sub Type property is not overwritten by its additional enum.Bird property, it still knows that enum.Bird is its sub-property.

Can you advise if I need to implement this line in Python, how can I do this?

I assume this would be pretty interesting knowledge for people learning Python, so I asked this question here for discussion. I am trying to think in C ++, but I did not understand this.

+1
source share
1 answer

I really don’t understand what the problem is. Consider the Enum defined in python:

 import enum class Type(enum.Enum): Bird = 0 Cat = 1 

Type.Bird and Type.Cat are instances of the Type class:

 >>> Type.Bird <Type.Bird: 0> >>> Type.Cat <Type.Cat: 1> 

So they have access to their own class, which is Type :

 >>> Type.Bird.__class__ <enum 'Type'> 

Now you can simply add the property class to the Type class and get this behavior:

 class Type(enum.Enum): Bird = 0 Cat = 1 @property def enum(self): return self.__class__ 

and now you have:

 >>> Type.Bird <Type.Bird: 0> >>> Type.Bird.enum <enum 'Type'> >>> Type.Bird.enum.Bird <Type.Bird: 0> >>> Type.Bird.enum.Cat <Type.Cat: 1> 

Note that while you can write Bird.enum , you will not be able to access, as in Type.enum , because this will return a property object.

To get the exact behavior that you see in this code, you can:

  • Set the settings.Type attribute as an instance of Type (possibly Invalid ) and do:

     def AddAnimalSettings(*args) settings = MyClass(*args) settings.Type = Type.Bird return settings 
  • Replace the use of property special descriptor that will handle access through the class. In this case, read the property documentation , which also provides the equivalent of its python code. The case you have to change is __get__ when obj is None :

     class MyProperty(object): # omissis def __get__(self, obj, objtype=None): if obj is None: return objtype # <-- changed this line if self.fget is None: raise AttributeError("unreadable attribute") return self.fget(obj) 

    Use it like:

     class Type(enum.Enum): Bird = 0 Cat = 1 @MyProperty def enum(self): return self.__class__ 

    And now you have:

     >>> Type.enum <enum 'Type'> 

    for Type.enum.Bird work.

+2
source

All Articles