This question already has answers here:
Create a struct by reflection in Go
(1 answer)
Is it possible to dynamically create a function with a receiver (method) in go?
(1 answer)
Can I create a new function using reflection in Go?
(4 answers)
Closed 8 months ago.
I would like to manipulate structs at Runtime.
For example, I have a struct:
type Item struct {
SomeField string
}
Is it possible to add field on runtime? Or Access Attribute that is not yet defined. Something like pythons __getattr__() or __call__() so I could dynamically control the fields/methods accessed.
E.g. do something like
Item.DynamicField or Item.DynamicMethod() where I don't know exactly the Field or the Method that will be accessed/called, So I can't define it statically.
Maybe I'm missing something in the Reflect package?
Thank you.
https://github.com/oleiade/reflections
The purpose of reflections package is to make developers life easier
when it comes to introspect structures at runtime. Its API is inspired
from python language (getattr, setattr, hasattr...) and provides
a simplified access to structure fields and tags.
Is it possible to add field on runtime? Or Access Attribute that is not yet defined.
No. Go is a compiled language with statically defined types. You probably need a map if you want to dynamically add properties.
Related
This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 3 years ago.
Is it possible to have protected class variables or methods in python? Can I see an example of such usage?
The short answer is "no." There are conventions and good style that allow you to indicate that someone shouldn't be modifying those variables or calling those methods from outside the class but there is no way to strictly enforce this. There essentially is no such thing as strictly enforced private or protected variables or methods in Python.
See this tutorial.
No it is not possible. People generally use underscores as a convention for private members.
This question on the general python convention can give some more information.
Python "private" function coding convention
Basically putting a '_' before your member name will indicate to the outside world that it is private.
not_private = 0
_private = 1
I am coming from a Java background and I am used to POJO to define a data model. Basically what I want to define is a data model using Python classes with strong attribute type checking and not being able to add more attribute then the one defined (basically something similar to django model). For example I am trying to do create a class:
class A:
element1: int
element2: str
but in this way I am not forcing that A object will not have attribute element3.
What is the pythonic way to achieve that, is there any framework you advice for python 3.6+?
You can do this using __slots__, you can find more information using the magic method __slots__ here: Slots docs.
Slots are a nice way to work around this space consumption problem. Instead of having a dynamic dict that allows adding attributes to objects dynamically, slots provide a static structure which prohibits additions after the creation of an instance.
Keep in mind that this comes at a cost.
It will break serialization (e.g. pickle). It will also break multiple inheritance. A class can't inherit from more than one class that either defines slots or has an instance layout defined in C code (like list, tuple or int).
I hope this information is sufficient.
This question already has answers here:
How to add comments to table or column in mysql using SQLAlchemy?
(3 answers)
Closed 4 months ago.
When creating a Column object with SQLAlchemy, you can specify a documentation string. Once set, how can this documentation be accessed in Python?
In addition, is using doc a good practise for documenting SQL Columns, or would it be better to use comment or perhaps Sphinx's standard for documenting general instance variables?
The doc parameter creates python docstrings for classes and fields of your ORM (see also the PEP 257). You can access them through the __doc__ attribute or through the built-in help() function. Probably some IDEs will use them for hints as well.
See this answer for some examples.
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 6 years ago.
I've recently learned about class, method, and (self) functions. While I seem to understand the syntax side of things, I find having to type [self] in front of variables bothersome with very little benefit.
In what scenario would this be beneficial compared to simply using individual functions without class reference?
In what scenario would this be beneficial compared to simply using individual functions without class reference?
whoa… what a question! I believe you should start with a lecture on object oriented programing.
Simply said: the benefits of using a class over laying out functions in a module, is to enable many features of object oriented programing (encapsulation of data through a behaviour, class inheritance, properties…).
And in more length: the main idea behind OOP, is that you create a public programing interface, that other developers (including you) will use, and you can hide the inner workings, so once the public interface is all implemented, you don't care how things work internally, and can even change without breaking the code that use that.
So in this case, you create a class, and you implement your own algorithms on your own data, but all what people really care is how to change your data using those algorithms.
And those algorithms are being called methods and the data members.
In many languages, when you create a method (i.e. a function that is bound to an "object"), the reference to the current instance of the object is implicit (and might be optionally explicit in Java or C++, which is the this variable).
In python, the language designers chose for it to be explicit, and called by convention self. Then in rare cases, you can choose to call it this if you want (which can be useful when doing nested classes).
Finally, I talked about "classes", "instances" and "objects". An object is an instance of a class. What that means is that the class is here to lay out how things work (what are the members, what are the methods…). Then you do instanciate your object by calling the constructor. So then, you can have plenty of objects that share the same class. Which means they work similarly, but they have different data.
But here I only scratch the surface, and only want to show you that there are a lot of concepts behind the self and the classes.
For more on the topic, go read books and take programming classes:
https://wiki.python.org/moin/BeginnersGuide
https://www.python.org/about/gettingstarted/
https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Classes
http://www.bodenseo.com/course/python_training_course.html
https://www.udemy.com/python-for-beginners/
https://www.coursera.org/learn/python
This question already has answers here:
What's the difference between a Python "property" and "attribute"?
(7 answers)
Closed 2 months ago.
Just a quick question, I'm having a little difficulty understanding where to use properties vs. where use to plain old attributes. The distinction to me is a bit blurry. Any resources on the subject would be superb, thank you!
Properties are more flexible than attributes, since you can define functions that describe what is supposed to happen when setting, getting or deleting them. If you don't need this additional flexibility, use attributes – they are easier to declare and faster.
In languages like Java, it is usually recommended to always write getters and setters, in order to have the option to replace these functions with more complex versions in the future. This is not necessary in Python, since the client code syntax to access attributes and properties is the same, so you can always choose to use properties later on, without breaking backwards compatibilty.
The point is that the syntax is interchangeable. Always start with attributes. If you find you need additional calculations when accessing an attribute, replace it with a property.
In addition to what Daniel Roseman said, I often use properties when I'm wrapping something i.e. when I don't store the information myself but wrapped object does. Then properties make excellent accessors.
Properties are attributes + a posteriori encapsulation.
When you turn an attribute into a property, you just define some getter and setter that you "attach" to it, that will hook the data access. Then, you don't need to rewrite the rest of your code, the way for accessing the data is the same, whatever your attribute is a property or not.
Thanks to this very clever and powerful encapsulation mechanism, in Python you can usually go with attributes (without a priori encapsulation, so without any getter nor setter), unless you need to do special things when accessing the data.
If so, then you just can define setters and getters, only if needed, and "attach" them to the attribute, turning it into a property, without any incidence on the rest of your code (whereas in Java, the first thing you usually do when creating a field, usually private, is to create it's associated getter and setter method).
Nice page about attributes, properties and descriptors here