Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set class names dynamically?

Tags:

python

I have a function that creates classes derived from it's arguments:

def factory(BaseClass) :     class NewClass(BaseClass) : pass     return NewClass 

Now when I use it to create new classes, the classes are all named the same, and the instances look like they have the same type:

NewA = factory(ClassA) NewB = factory(ClassB) print type(NewA()) # <class __main__.NewClass> print type(NewB()) # <class __main__.NewClass> 

Is the proper fix to manually set the __name__ attribute?

NewA.__name__ = 'NewA' print type(NewA()) # <class __main__.NewA> 

Are there any other things I should be setting while I'm at it?

like image 268
kiyo Avatar asked Mar 18 '11 13:03

kiyo


People also ask

How we can set dynamic class names in React?

Set Dynamic className Values in React All you have to do is wrap JavaScript expressions with curly brackets {} , and everything between these brackets will be evaluated. You can set multiple values to the className attribute. Some of them will be constant, while others will be applied dynamically.

How do you add a dynamic name to a class in HTML?

Try class$="{{v1bg}}" , as this will bind to the class attribute rather than the class property.

How do I add a class dynamically?

You can use Javascript for adding a class: setAttribute and className both method are used to set class (class attribute), these method are not used to adding another class with existing one.

How do you dynamically change CSS class in React?

Use a template literal to dynamically add CSS classes to React elements. Template literals are delimited with backticks and allow us to embed variables and expressions using the dollar sign and curly braces ${expression} syntax. Copied!


2 Answers

Yes, setting __name__ is the correct thing to do; you don't need to set anything else to adjust the class name.

For example:

def factory(BaseClass) :     class NewClass(BaseClass): pass     NewClass.__name__ = "factory_%s" % BaseClass.__name__     return NewClass 

type is the wrong thing to use here. It doesn't let you define classes with Python's normal class syntax, instead making you set up every class attribute manually. It's used to create classes by hand, e.g. if you have an array of base classes and you want to create a class using it (which you can't do with Python's class syntax). Don't use it here.

like image 81
Glenn Maynard Avatar answered Sep 28 '22 19:09

Glenn Maynard


Updating the answer off Glenn Maynard: Nowadays there is the __name__ attribute and the __qualname__ attribute. The first is what you might think; the second is the dotted "path" for nested classes.

In case of "simple" classes both are equal. Just set __name__ and __qualname__ to your new name. You should set both attributes, since you cannot be sure at which one 3rd-party code will look.

Now for nested classes, the differences between the two attributes show:

class Outer:     class Inner:         pass print(Outer.__name__, Outer.__qualname__) print(Outer.Inner.__name__, Outer.Inner.__qualname__) 

prints:

Outer Outer Inner Outer.Inner 

If you want to change Outer's name, you need to patch three places, namely Outer.__name__, Outer.__qualname__, Inner.__qualname__. For the latter two you need to split and join at the dots correctly.

A final warning: Even if you did all that right, stuff like sphinx, pylint, etc... might still not work 100%. For example the fake name cannot be found in the module namespace as usual; the source cannot be grepped for the class definition; and so on.

like image 34
Torben Klein Avatar answered Sep 28 '22 19:09

Torben Klein