Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions xml_models/xml_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class ModelBase(type):
"""

def __new__(mcs, name, bases, attrs):
if name == 'Model':
return type.__new__(mcs, name, bases, attrs)

new_class = super(ModelBase, mcs).__new__(mcs, name, bases, attrs)
xml_fields = [field_name for field_name in attrs.keys() if isinstance(attrs[field_name], BaseField)]
setattr(new_class, 'xml_fields', xml_fields)
Expand All @@ -222,8 +225,8 @@ def __new__(mcs, name, bases, attrs):
return new_class

def _get_xpath(cls, field_impl):
return property(fget=lambda cls: cls._parse_field(field_impl),
fset=lambda cls, value: cls._set_value(field_impl, value))
return property(fget=lambda self: self._parse_field(field_impl),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this changed to self? cls is convention in meta classes and wouldn't self be undefined as the first param is cls?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are setting property for models that Inherit from xml_models.Model(or models has ModelBase as metaclass, but we use function in xml_models.Model, so it has to be models that inherit it), the setter/getter function will not be called right now, the setter and getter function will be called on those instances later while we are serializing xml into python objects and using those data, and the self represents a particular instance object.

The lambda expression just define the property, and the keyword cls(should be self though) in the lambda expression has nothing to do with the MetaClass. so i changed it to self. those self is underfined situation will not happen:)

fset=lambda self, value: self._set_value(field_impl, value))


from future.utils import with_metaclass
Expand Down