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.
Related
This question already has answers here:
Being that string substitution is frowned upon with forming SQL queries, how do you assign the table name dynamically?
(2 answers)
Closed 1 year ago.
pretty basic question here. In python, using sqlite3, I'm able to prevent most sql injections with ? placeholders. However this option doesn't exist for table names.
In most scenarios it's pretty easy for me to just check if the table exists, however I also need tables to be created from placeholders that will be user supplied.
How would I go about doing this?
edit: Honestly the associated question really doesn't help in this situation... The issue here is that I want a user to supply their own table name. Regardless I decided to use a scheme that I described in my own answer here so I don't really care about this question anymore.
Well it might not be best practice but my solution will just be storing the tables and column names as Table1, Column1, etc. I can store the names separately.
This question already has answers here:
What's the difference between a method and a function?
(41 answers)
Closed 4 years ago.
I am all new to python and I just got to know methods I am using python 3.7.0 64 bit and I want to know what is the difference between methods and functions. but please in very very basic explanation because I am also all new to programming.
Let's have two examples
len(), and list.sort()
Here len() is called as function because it is invoked before declaring the object on which it is to be used.
On the other hand, we would call sort as a method of list object.
The difference between methods and functions is that functions are declared in a class, whereas methods are declared in an object.
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.
This question already has answers here:
Does Python have “private” variables in classes?
(15 answers)
Closed 9 years ago.
I have come from c++ and java background so, I was curious to know if python provides access specifiers as provided by c++/java. I've seen some code, and this is what I think,
__variable ---> private.
_variable ----> protected.
Correct me if I'm wrong.
Python has recommended practices rather than prescriptive ones - so anything with a _ at the start should be left alone by others but is not locked to prevent it. There is however name mangling to make life more interesting for members with a __ at the start - see PEP8.
Of course if others rely on your private data/methods rather then the public API they only have themselves to blame when you change something and their code stops working.
There is no such concept in Python. There are conventions that are used - like the ones Steve mentioned but also others such as calling the first variable of an instance method self.
In addition, for module level imports - there is a way to prevent the default behavior of importing all names from a module. This is done by populating __all__ with a list of names that should be imported (exposed) by default.
However, as with __var and _var it is just a convention (although one that is enforced by Python). It doesn't restrict you though - you can explicitly import any name.
This question already has answers here:
Should I use 'has_key()' or 'in' on Python dicts? [duplicate]
(9 answers)
Closed last month.
There are several different ways to check if a Python dictionary contains a specific key, i.e.
d = {}
if key in d:
if d.contains(key):
if d.has_key(key):
it's silly for a language to allow you to do the same thing several different ways, UNLESS, each of the methods was doing something entirely different. Could someone please contrast the three techniques above, how are they different?
They're all the same and they're all around for historical reasons, but you should use key in d.
Method #1 is the accepted way to do it. Method #2 doesn't actually exist, at least in any versions of Python that I'm aware of; I'd be interested to see where you found that. Method #3 used to be the accepted way, but is now deprecated.
So there really is just one way.
d.__contains__(key) is what is used it key in d (since in operator calls __contains__ method of the dictionary)
has_key is deprecated and does the same as __contains__
key in d is the accepted way to do it.
__contains__ is the ‘“magic” attribute’ (ref) that implements the above syntax. Most, if not all, special syntax is implemented via such methods. E.g., the with statement is implemented via __enter__ and __exit__. Such methods exist for allowing special functionality to be provided for user-defined classes.
the has_key method no longer exists in Python 3 and is deprecated in Python 2.