pointer and python

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Oct 18 00:17:09 EDT 2013


On Fri, 18 Oct 2013 06:43:12 +0330, Mohsen Pahlevanzadeh wrote:

> On Fri, 2013-10-18 at 00:54 +0100, MRAB wrote:
[...]
>> Why are you using attributes anyway? Why not just store them in a dict?
>> 
>> 
> At first i store them in a dict:
> ###########################################33
>         for row in xrange(len(uniqueFields)  ):
> 111111111111instance = QtGui.QCheckBox(uniqueFields[row])
> 
111111111111projectsFindInstance.projectsInstance.addOnFieldsInstance.update
({uniqueFields[row]:instance})
> 111111111111projectsFindInstance.tableWidget.setCellWidget
(row,0,instance)
>      
> 111111111111projectsFindInstance.tableWidget.setRowCount
(projectsFindInstance.tableWidget.rowCount()+1)
> ###########################3
> above 1 digits are intending...!

Why??? What is the purpose of all those 1 digits?


By the way, you should not write code like this:

# uniqueFields is a dictionary or list
for row in xrange(len(uniqueFields)):
    process(uniqueFields[row])

This adds completely unnecessary layer of redirection. Think about real 
life: if I asked you to "move those books on the shelf", would you think 
about it as:

* move each book in turn

or would you think of it as:

* count the number of books
* starting at zero, and continuing up to but not including the 
  number you just counted, set the value of the counter N
* move the book in position N


In this case, it is better to not use "row" at all, but just apply 
directly to the entities in the dict:

for field in uniqueFields:
    process(field)


If you need the row number as well:

for row, field in enumerate(uniqueFields):
    process(field, row)


> At first i store them as QCheckBox object in a dict, then i need to
> create connect function according them:
> ################################################################
>         for key, val in
> self.projectsInstance.addOnFieldsInstance.items():
>             setattr(self,"%s" % val,val)
>             instance = getattr(self,"%s" % val)

What is the purpose of these lines? You already have the value that you 
want, it is val. Why bother setting the value as an attribute, using 
itself as the attribute name, then reading the attribute name, only to 
get the *same value that you started with*?

This is what you do:

* fetch a value from the dict, which gives object foo
* assign val = foo
* create an attribute self.foo = foo
* look up attribute self.foo, which gives foo
* finally use foo

Two of those lines are pointless. You already had object foo. Just use it:

* fetch a value from the dict, which gives object foo
* assign val = foo
* use foo



> ##########################
> setttr and getattr and connect are in for loop.
> 
> But i have serious question:
> how i can retrive content of a varibale such as C pointer: p = *x ;

You can't, and you don't need to. Python is not C. If you try to write 
code in Python like you would write it in C, you'll end up with slow, 
buggy code.


> setattr(self,"%s" %val , *val) ==> i need to paste content of content of
> val variable.

What does "paste content" mean in this case? I do not understand your 
question. What do you think "content of content" of val means?


-- 
Steven



More information about the Python-list mailing list