Giter VIP home page Giter VIP logo

Comments (6)

biqqles avatar biqqles commented on July 21, 2024

Thanks for the question. As you pointed out, mutable defaults are copied, so for example list_field = field(default_factory=list) in dataclasses becomes simply list_field: List = [] with dataclassy. Or, as a more complicated example, dd_field: Dict = field(default_factory=lambda: defaultdict(list)) becomes dd_field: Dict = defaultdict(list).

I couldn't think of a use case this alternative mechanism wouldn't cover. Do you have one in mind?

from dataclassy.

metaperl avatar metaperl commented on July 21, 2024

I couldn't think of a use case this alternative mechanism wouldn't cover. Do you have one in mind?

What if I have a field that will contain instances of MyCustomClass?

How would I specify this?

from dataclassy.

biqqles avatar biqqles commented on July 21, 2024

If you define a copy method for the class it should work.

from dataclassy.

biqqles avatar biqqles commented on July 21, 2024

I suppose this naturally raises the question of whether this is ideal. My thinking, as implied earlier, is that in the clear majority of cases where a default value can't be shared between instances it is for the reason that it is mutable, so copying it will suffice. In the case of collections, this is a clear advantage (imo) since it reduces the repetitiveness of lambdas. This would definitely be a disadvantage if you commonly wanted to create class instances, particularly if you wanted to create instances of classes you don't control.

Another option to the above solution would be to use post-initialisation logic:

@dataclass
class Example:
    mcc: MyCustomClass = None

    def __init__(self, mcc=None):
        self.mcc = MyCustomClass()

This has the major advantage of flexibility - you can do logic as complex as you like in there, including initialising MyCustomClass based on other fields of the dataclass or additional parameters to __init__. And of course, there is no need to define a copy method.

I admit that this is still not as "nice" as dataclasses' default_factory. However, adding a field clone would go almost violently against dataclassy's ethos of radical simplicity in its own codebase, especially as this is essentially the only parameter that there won't be a lighter-weight equivalent for. That's the reason it's not there already. I'm still very interested in hearing people's thoughts on this though.

from dataclassy.

biqqles avatar biqqles commented on July 21, 2024

As of 03cebc9 this is now possible with

@dataclass
 class Example:
     mcc: MyCustomClass = None
 
     def __init__(self):
         self.mcc = MyCustomClass()

and an example has been added to the README to document this.

from dataclassy.

biqqles avatar biqqles commented on July 21, 2024

For future readers, as of release v0.9, this is now possible:

@dataclass
class Example:
    mcc: MyCustomClass = factory(MyCustomClass)

from dataclassy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.