Problem with optional parameter of function in classes

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
DjangoFreeCAD
Posts: 24
Joined: Mon Dec 31, 2018 6:43 pm
Location: France

Problem with optional parameter of function in classes

Post by DjangoFreeCAD »

Suppose you have the classes (simplidied code)

Code: Select all

[size=200]

class C_Window(QtGui.QDialog):
    def __init__(self):
        super(C_Window, self).__init__()
   def createLabel(self, x, y, text, fontName="" , fontSize=18 , style=0):
        l...ines of code...   
  
class C_HelpWindow(M_Window.C_Window):
    def __init__(self):
        super(C_HelpWindow,self).__init__()
        self.initUI()
    def initUI(self):
        ...other lines of code...
        self.labelTitre = self.createLabel(100,30, "Titre")
        >>> no error
        self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)
        >>> error
        
[/size]
After working a lot of time on this issue, I discover that the compiler is adding in the parameters given to the createLabel function during the call
a reference on self before the parameters (this is the normal behavior), but also another ref to self before passing the optional parameters.

The first optional parameter, when given in the calling code, is hidden by 'self'

The solution, add an optional placeHolder parameter in the funtion definition It will be hidden by 'self' but as it is not used, it does'nt matter

The new code which works

Code: Select all

[size=200]

class C_Window(QtGui.QDialog):
    def __init__(self):
        super(C_Window, self).__init__()
   def createLabel(self, x, y, text, placeHolder=0, fontName="" , fontSize=18 , style=0):
        l...ines of code...   
  
class C_HelpWindow(M_Window.C_Window):
    def __init__(self):
        super(C_HelpWindow,self).__init__()
        self.initUI()
    def initUI(self):
        ...other lines of code...
        self.labelTitre = self.createLabel(100,30, "Titre")
        >>> no error
        self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)
        >>> no error 
        
[/size]
Has anyone had the same problem ?
-----------------------------------
OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13541 (Git)
Build type: Release
Branch: releases/FreeCAD-0-17
Hash: 9948ee4f1570df9216862a79705afb367b2c6ffb
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: French/France (fr_FR)
User avatar
amrit3701
Posts: 343
Joined: Mon Jun 13, 2016 5:37 pm

Re: Problem with optional parameter of function in classes

Post by amrit3701 »

DjangoFreeCAD wrote: Mon Jan 21, 2019 11:27 am Suppose you have the classes (simplidied code)

Code: Select all

[size=200]

class C_Window(QtGui.QDialog):
    def __init__(self):
        super(C_Window, self).__init__()
   def createLabel(self, x, y, text, fontName="" , fontSize=18 , style=0):
        l...ines of code...   
  
class C_HelpWindow(M_Window.C_Window):
    def __init__(self):
        super(C_HelpWindow,self).__init__()
        self.initUI()
    def initUI(self):
        ...other lines of code...
        self.labelTitre = self.createLabel(100,30, "Titre")
        >>> no error
        self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)
        >>> error
        
[/size]
Which error comes after running self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)?

After working a lot of time on this issue, I discover that the compiler is adding in the parameters given to the createLabel function during the call
a reference on self before the parameters (this is the normal behavior), but also another ref to self before passing the optional parameters.

The first optional parameter, when given in the calling code, is hidden by 'self'

The solution, add an optional placeHolder parameter in the funtion definition It will be hidden by 'self' but as it is not used, it does'nt matter

The new code which works
I didn't get. Can you elaborate it more in detail? Also, I suggest to use FreeCAD build with Python3 and QT5.
Amritpal Singh
Github, Like my work, sponsor me!
DjangoFreeCAD
Posts: 24
Joined: Mon Dec 31, 2018 6:43 pm
Location: France

Re: Problem with optional parameter of function in classes

Post by DjangoFreeCAD »

Amritpal,

the error was fired in the following lines of code saying that the parameters in a called function setFont were'nt of the right type example None and not str in the function self.label.setFont(fontName)

Another important information, the problem doesn't occur in one simple class, but when the function is in the parent class and used by the child one

in my exemple the HelpWindow class use a function of the CWindow Class

How can I Get and install Python3, i used the standard v0.17 download package on this site ?
Last edited by DjangoFreeCAD on Tue Jan 22, 2019 6:32 pm, edited 1 time in total.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Problem with optional parameter of function in classes

Post by DeepSOIC »

I have tested this:

Code: Select all

from PySide import QtGui

class C_Window(QtGui.QDialog):
    def __init__(self):
        super(C_Window, self).__init__()
    def createLabel(self, x, y, text, fontName="" , fontSize=18 , style=0):
        print(self,x,y,text,fontName,fontSize,style)

class C_HelpWindow(C_Window):
    def __init__(self):
        super(C_HelpWindow,self).__init__()
        self.initUI()
    def initUI(self):
        self.labelTitre = self.createLabel(100,30, "Titre")
        self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)

dialog = C_HelpWindow()
And the result is exactly as expected:

Code: Select all

(<__main__.C_HelpWindow object at 0x0000020EA141D908>, 100, 30, 'Titre', '', 18, 0)
(<__main__.C_HelpWindow object at 0x0000020EA141D908>, 30, 60, 'Explanations', '', 10, 0)
Notice, that in your code, I changed the inheritance of the second class: yours inherits from M_Window.C_Window, but mine just from just C_Window. I assume these are two pieces of code you ripped out of different modules, but I decided to explain it, just in case.

Tested on:
OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15252 (Git)
Build type: Release
Branch: master
Hash: 652e1dbdd0fb74f1d3df8034254281df9c24aae6
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: Russian/RussianFederation (ru_RU)

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15518 (Git)
Build type: Release
Branch: master
Hash: e83c44200ab428b753a1e08a2e4d95e03236e481
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Russian/Russia (ru_RU)
User avatar
amrit3701
Posts: 343
Joined: Mon Jun 13, 2016 5:37 pm

Re: Problem with optional parameter of function in classes

Post by amrit3701 »

DjangoFreeCAD wrote: Tue Jan 22, 2019 5:16 pm Amritpal,

the error was fired in the following lines of code saying that the parameters in a called function setFont were'nt of the right type example None and not str in the function self.label.setFont(fontName)
On mine side, it works expected.

Added some print statement from debugging and change inheritance of C_HelpWindow from M_Window.C_Window to C_Window to your code.

Code: Select all

class C_Window(QtGui.QDialog):
    
    def __init__(self):
        super(C_Window, self).__init__()
    
    def createLabel(self, x, y, text, fontName="" , fontSize=18 , style=0):
        print('Create Label called')
        print('x', x)
        print('y', y)
        print('text', text)
        print('fontName', fontName)
        print('fontSize', fontSize)
        print('---end---')
  

class C_HelpWindow(C_Window):
    def __init__(self):
        super(C_HelpWindow,self).__init__()
        self.initUI()
        
    def initUI(self):
        self.labelTitre = self.createLabel(100,30, "Titre")
        self.labelTitre1 = self.createLabel(30,60, "Explanations", fontSize = 10)
        
d = C_HelpWindow()
Output:

Code: Select all

Create Label called
x 100
y 30
text Titre
fontName 
fontSize 18
---end---
Create Label called
x 30
y 60
text Explanations
fontName 
fontSize 10
---end---

To me, it looks like you misunderstand something. Better to share your full example. :)

I have tried above example on below FreeCAD version:

OS: macOS 10.14
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.Unknown
Build type: Release
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/India (en_IN)

How can I Get Python3, i used the standard v0.17 download package on this site ?
https://github.com/FreeCAD/FreeCAD/releases


Thanks,
Amritpal Singh
Github, Like my work, sponsor me!
DjangoFreeCAD
Posts: 24
Joined: Mon Dec 31, 2018 6:43 pm
Location: France

Re: Problem with optional parameter of function in classes

Post by DjangoFreeCAD »

DeepSoic,

Exactly, the 2 classes are in 2 diferent files, and also the C_Window class is not in the same folder, but in a sub directory inside the macro folder
(macroFolder/PyUtils) and at the beginning of the script i put these lines to add during this script the folder to the pyton path

import os, sys
scriptDir = os.path.dirname(os.path.realpath(__file__))
utilsDirName = "PyUtils"
pyUtilsDir = scriptDir + "/" + utilsDirName
if (utilsDirName in sys.path) == False : sys.path.insert(0, pyUtilsDir)
DjangoFreeCAD
Posts: 24
Joined: Mon Dec 31, 2018 6:43 pm
Location: France

Re: Problem with optional parameter of function in classes

Post by DjangoFreeCAD »

DeepSoic, Amrit,

I tried your modified code with my files and the problem does'nt happen !

Thank you for your help, I'm going to investigate what happened before (I had changed the code with my workaround before I posted
Post Reply