Python Forum
declaring object parameters with type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
declaring object parameters with type
#1
When I pass an object in as a parameter to a function, is there a way to specify in the function declaration what type the parameter is so that I can see its properties as I type. I have a class called Browser
br=Browser()
br.<argument and type display here>
print(other_function(br))
other_function(brw):
    x=brw.<here I get nothing>
    return True
Is there any way to do a sort of early binding. What I have been doing is to over-shadow the parameter with a local instance of its class, then when I am done coding the function, I delete it
Reply
#2
You can use type annotations
def other_function(brw: Browser):
    x = brw <auto complete now knows to look at Browser>
This will also help when when calling other_function
other_function(<auto complete will tell you a Browsere object is expected
Reply
#3
(Dec-13-2022, 07:45 PM)deanhystad Wrote: You can use type annotations
def other_function(brw: Browser):
    x = brw <auto complete now knows to look at Browser>
This will also help when when calling other_function
other_function(<auto complete will tell you a Browsere object is expected

Awesome. Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Type Error: 'in' object is not callable nman52 3 3,419 May-01-2021, 11:03 PM
Last Post: nman52
  Please explain uncommon way of declaring and using variable [function.variable] esphi 4 2,343 Nov-07-2020, 08:59 AM
Last Post: buran
  Python Class and Object Parameters JoeDainton123 5 2,906 Sep-02-2020, 10:43 AM
Last Post: JoeDainton123
  TypeError: 'type' object is not subscriptable Stef 1 4,543 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  isinstance() always return true for object type check Yoki91 2 2,578 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 2,252 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn
  Type hinting - return type based on parameter micseydel 2 2,506 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Object of type set is not JSON serializable enigma619 5 12,273 Dec-19-2019, 03:30 PM
Last Post: enigma619
  type of object as a string Skaperen 1 2,298 Dec-02-2019, 08:01 AM
Last Post: midarq
  Declaring a Global Variable in a Function Bob1948 4 3,047 Sep-14-2019, 11:16 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020